【问题标题】:Path of Image to be passed in Uri.parse to open an image using intent要在 Uri.parse 中传递以使用意图打开图像的图像路径
【发布时间】:2016-06-18 06:16:14
【问题描述】:

我正在尝试使用意图打开图像。 图片在我的存储中的路径是/storage/emulated/0/nitp/download/logo.png

我的代码是

Intent intent = new Intent();
intent.setAction(Intent.ACTION_DEFAULT);
intent.setDataandType(Uri.parse("/storage/emulated/0/nitp/download/logo.png"),"image/*");
startActivity(intent);

我也试过把 file://storage/emulated/0/nitp/download/logo.pngcontent://storage/emulated/0/nitp/download/logo.png

我应该使用什么路径?

已解决 必须使用 file:///storage/emulated/0/nitp/downloads/logo.png

【问题讨论】:

  • 它提示“选择应用程序”对话框,但我选择的应用程序没有打开。它立即关闭,我再次进入我的应用活动
  • 你在调用startActivity后在logcat上看到了什么?
  • 还有adb shell ls -l /storage/emulated/0/nitp/download/logo.png的输出是什么?
  • 当我尝试添加 shell ls storage/emulated/0/nitp/downloads 时,它说没有这样的目录。但相反,它显示该目录是 /storage/sdcard0/nitp/downloads。

标签: android android-intent


【解决方案1】:

对于从媒体存储或 SD 卡中选择图像:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

对于获取Image的路径并在ImageView中设置:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            //Getting the Bitmap from Gallery
            imgpath= MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            //Setting the Bitmap to ImageView
            imageViewProfileImage.setImageBitmap(imgpath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

希望这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 2020-06-18
    • 1970-01-01
    • 2012-06-08
    • 2013-08-30
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多