Android: 如何从图库打开图片?

3

我在我的应用程序中创建了一个方法,可以将我的布局保存为图像并在画廊中显示。

我正在尝试打开它,但是我对所有教程和不同的方法感到困惑...

这是我的代码:

SimpleDateFormat mTimeFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date mTime = new Date();
        String mStringTime = mTimeFormat.format(mTime);

        String fileFolder = "/orders";
        String fileName = "/order-"+mStringTime;
        File filePath = new File("/sdcard"+fileFolder+fileName+".png");
        Uri fileUri = Uri.fromFile(filePath);

        File createFolder = new File(Environment.getExternalStorageDirectory()+fileFolder);
        if (!createFolder.exists()){ //checks I don't create the same folder twice
            createFolder.mkdir();
        }

        View orderView = findViewById(R.id.tableOrder); //this is what i shall save
        orderView.setDrawingCacheEnabled(true);
        Bitmap bitmap = orderView.getDrawingCache();      

        try {
            FileOutputStream outputImage = new FileOutputStream(filePath);
            bitmap.compress(CompressFormat.PNG, 10, outputImage);
            outputImage.close();
            orderView.invalidate();     

            Intent intentScanner = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intentScanner.setData(fileUri);
            sendBroadcast(intentScanner);

        } 
        catch (Exception e) 
        { e.printStackTrace();
        } finally{
            orderView.setDrawingCacheEnabled(false);  

            Intent intentView = new Intent(Intent.ACTION_VIEW);
            intentView.setType("image/*");
            intentView.setData(fileUri);
            startActivity(intentView);
        }

应用程序崩溃时,图像已被保存。

android.content.ActivityNotFoundException: No Activity found to handle Intent

权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我该怎么办?

2个回答

7
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, <unique_code>);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == <unique_code>) {
        imageView.setImageBitmap(getPicture(data.getData()));
    }
}

public static Bitmap getPicture(Uri selectedImage) {
    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();
    return BitmapFactory.decodeFile(picturePath);
}

我真的不太明白。我该如何获取你在onActivityResult中使用的imageView?我不想显示我的图片,我想将其作为图库的一部分打开(即向图库应用发送意图)。 - EladB
抱歉,我的错...我太快读了你的问题。我提供的示例是用于检索从图库中选择的图像。 - mbmc

1
从您的代码中打开画廊应用程序,请调用此函数。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
     "content://media/internal/images/media")); 
     startActivity(intent);

编辑:

打开最近保存的图像

public void openInGallery(String imageId) {
  Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.buildUpon().appendPath(imageId).build();
  Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  startActivity(intent);
}

你所需要做的就是将图像 ID 追加到 EXTERNAL_CONTENT_URI 的路径末尾。然后使用 View 操作启动 Intent,以及 Uri。
图像 ID 来自于查询内容解析器。

它如何帮助我?我想打开我刚刚创建的图像 - 不是整个画廊... - EladB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接