Android更换头像弹框之createChooser
博客专区 > sizeof 的博客 > 博客详情
Android更换头像弹框之createChooser
sizeof 发表于3年前
Android更换头像弹框之createChooser
  • 发表于 3年前
  • 阅读 44
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: 扒了一个代码,看看有价值的部分吧

先看看恶心的smali,简单分析了一下

.method public static takeOrChoosePhoto(Landroid/app/Activity;I)V
    .locals 14
    .param p0, "context"    # Landroid/app/Activity;
    .param p1, "requestCode"    # I


    .prologue
    .line 148
    sget-object v11, Lcn/zhimawu/utils/ImageUtils;->PHOTO_DIR:Ljava/io/File;


    invoke-virtual {v11}, Ljava/io/File;->mkdirs()Z


    .line 149
    new-instance v11, Ljava/io/File;


    sget-object v12, Lcn/zhimawu/utils/ImageUtils;->PHOTO_DIR:Ljava/io/File;


    invoke-static {}, Lcn/zhimawu/utils/ImageUtils;->getPhotoFileName()Ljava/lang/String;


    move-result-object v13


    invoke-direct {v11, v12, v13}, Ljava/io/File;-><init>(Ljava/io/File;Ljava/lang/String;)V


    sput-object v11, Lcn/zhimawu/utils/ImageUtils;->mCurrentPhotoFile:Ljava/io/File;


    .line 150
    sget-object v11, Lcn/zhimawu/utils/ImageUtils;->mCurrentPhotoFile:Ljava/io/File;


    invoke-static {v11}, Landroid/net/Uri;->fromFile(Ljava/io/File;)Landroid/net/Uri;


    move-result-object v7


    .line 152
    .local v7, "outputFileUri":Landroid/net/Uri;
    
    创建LIST V0 叫cameraIntents
    
    new-instance v0, Ljava/util/ArrayList;


    invoke-direct {v0}, Ljava/util/ArrayList;-><init>()V


    .line 153
    .local v0, "cameraIntents":Ljava/util/List;, "Ljava/util/List<Landroid/content/Intent;>;"
    
    创建V1 captureIntent
    
    new-instance v1, Landroid/content/Intent;


    const-string v11, "android.media.action.IMAGE_CAPTURE"


    初始化
    
    invoke-direct {v1, v11}, Landroid/content/Intent;-><init>(Ljava/lang/String;)V


    .line 154
    .local v1, "captureIntent":Landroid/content/Intent;
    
    p0 是这个takeOrChoosePhoto方法的参数 context
    
    invoke-virtual {p0}, Landroid/app/Activity;->getPackageManager()Landroid/content/pm/PackageManager;


    返回给V8
    move-result-object v8


    .line 155
    .local v8, "packageManager":Landroid/content/pm/PackageManager;
    const/4 v11, 0x0


    invoke-virtual {v8, v1, v11}, Landroid/content/pm/PackageManager;->queryIntentActivities(Landroid/content/Intent;I)Ljava/util/List;


    move-result-object v6


    .line 156
    .local v6, "listCam":Ljava/util/List;, "Ljava/util/List<Landroid/content/pm/ResolveInfo;>;"
    invoke-interface {v6}, Ljava/util/List;->iterator()Ljava/util/Iterator;


    move-result-object v4


    .local v4, "i$":Ljava/util/Iterator;
    :goto_0
    invoke-interface {v4}, Ljava/util/Iterator;->hasNext()Z


    move-result v11


    if-eqz v11, :cond_0


    invoke-interface {v4}, Ljava/util/Iterator;->next()Ljava/lang/Object;


    move-result-object v10


    check-cast v10, Landroid/content/pm/ResolveInfo;


    .line 157
    .local v10, "res":Landroid/content/pm/ResolveInfo;
    iget-object v11, v10, Landroid/content/pm/ResolveInfo;->activityInfo:Landroid/content/pm/ActivityInfo;


    iget-object v9, v11, Landroid/content/pm/ActivityInfo;->packageName:Ljava/lang/String;


    .line 158
    .local v9, "packageName":Ljava/lang/String;
    new-instance v5, Landroid/content/Intent;


    invoke-direct {v5, v1}, Landroid/content/Intent;-><init>(Landroid/content/Intent;)V


    .line 159
    .local v5, "intent":Landroid/content/Intent;
    new-instance v11, Landroid/content/ComponentName;


    iget-object v12, v10, Landroid/content/pm/ResolveInfo;->activityInfo:Landroid/content/pm/ActivityInfo;


    iget-object v12, v12, Landroid/content/pm/ActivityInfo;->packageName:Ljava/lang/String;


    iget-object v13, v10, Landroid/content/pm/ResolveInfo;->activityInfo:Landroid/content/pm/ActivityInfo;


    iget-object v13, v13, Landroid/content/pm/ActivityInfo;->name:Ljava/lang/String;


    invoke-direct {v11, v12, v13}, Landroid/content/ComponentName;-><init>(Ljava/lang/String;Ljava/lang/String;)V


    invoke-virtual {v5, v11}, Landroid/content/Intent;->setComponent(Landroid/content/ComponentName;)Landroid/content/Intent;


    .line 161
    invoke-virtual {v5, v9}, Landroid/content/Intent;->setPackage(Ljava/lang/String;)Landroid/content/Intent;


    .line 162
    const-string v11, "output"


    invoke-virtual {v5, v11, v7}, Landroid/content/Intent;->putExtra(Ljava/lang/String;Landroid/os/Parcelable;)Landroid/content/Intent;


    .line 163
    invoke-interface {v0, v5}, Ljava/util/List;->add(Ljava/lang/Object;)Z


    goto :goto_0


    .line 167
    .end local v5    # "intent":Landroid/content/Intent;
    .end local v9    # "packageName":Ljava/lang/String;
    .end local v10    # "res":Landroid/content/pm/ResolveInfo;
    :cond_0
    
    
    创建intent
    
    new-instance v3, Landroid/content/Intent;


    invoke-direct {v3}, Landroid/content/Intent;-><init>()V


    设置images/*,setType


    .line 168
    .local v3, "galleryIntent":Landroid/content/Intent;
    const-string v11, "image/*"


    invoke-virtual {v3, v11}, Landroid/content/Intent;->setType(Ljava/lang/String;)Landroid/content/Intent;


    设置ACTION为android.intent.action.GET_CONTENT


    .line 169
    const-string v11, "android.intent.action.GET_CONTENT"


    invoke-virtual {v3, v11}, Landroid/content/Intent;->setAction(Ljava/lang/String;)Landroid/content/Intent;


    创建createChooser


    .line 172
    const-string v11, "\u9009\u62e9\u7167\u7247" 这4个字符是选择照片


    invoke-static {v3, v11}, Landroid/content/Intent;->createChooser(Landroid/content/Intent;Ljava/lang/CharSequence;)Landroid/content/Intent;


    将结果返回给V2变量
    
    move-result-object v2
    
    
    
    设置createChooser的返回结果(Intent)的chooserIntent为android.intent.extra.INITIAL_INTENTS


    声明变量chooserIntent
    .line 175
    .local v2, "chooserIntent":Landroid/content/Intent;
    
    声明变量V12为字符串
    const-string v12, "android.intent.extra.INITIAL_INTENTS"


    invoke-interface {v0}, Ljava/util/List;->size()I


    move-result v11


    new-array v11, v11, [Landroid/os/Parcelable;


    invoke-interface {v0, v11}, Ljava/util/List;->toArray([Ljava/lang/Object;)[Ljava/lang/Object;


    move-result-object v11


    check-cast v11, [Landroid/os/Parcelable;


    v2.putExtra(v12,v11)


    invoke-virtual {v2, v12, v11}, Landroid/content/Intent;->putExtra(Ljava/lang/String;[Landroid/os/Parcelable;)Landroid/content/Intent;


    .line 178
    invoke-virtual {p0, v2, p1}, Landroid/app/Activity;->startActivityForResult(Landroid/content/Intent;I)V


    .line 179
    return-void
.end method


事实上当你进行打开相册的intent的时候可以有很多种选择,

//                        这个Intent是远古方法 直接进入相册里面选择了
//                        Intent intent = new Intent(Intent.ACTION_PICK, null);
//                        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
//                        startActivityForResult(intent,1);



//                        这个Intent是弹出 选择要使用的应用(图库、文件管理、WPS等)
//                        Intent intent = new Intent(
//                                Intent.ACTION_GET_CONTENT);
//                        intent.addCategory(Intent.CATEGORY_OPENABLE);
//                        intent.setType("image/*");



这个也是直接进入相册中选择
//                        Intent intent = new Intent(Intent.ACTION_PICK);
//                        intent.setType("image/*");//相片类型



但是需要弹出个框子选择可以拍照、还可以图库、文件管理的,就需要这样了(我没按照原版smali代码):

Intent captureIntent = new Intent("android.media.action.IMAGE_CAPTURE");

                        Parcelable[] parcelables = new Parcelable[]{captureIntent};

                        Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                        galleryIntent.setType("image/*");
                        Intent chooserIntent = Intent.createChooser(galleryIntent, "选择照片");
                        chooserIntent.putExtra("android.intent.extra.INITIAL_INTENTS", parcelables);


                        startActivityForResult(chooserIntent, 1);



OK、测试机型是小米

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 4
博文 18
码字总数 11300
×
sizeof
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: