简体   繁体   English

Android:如何从图库中打开图像?

[英]Android: how to open image from gallery?

I've created a method in my app that saves my layout as an image and presents it in the gallery. 我在应用程序中创建了一个方法,该方法将布局保存为图像并将其显示在图库中。 I'm trying to open it but I'm having a hard time with all the tutorials and different approaches... 我正在尝试打开它,但是我在使用所有教程和不同方法时都遇到了困难。

This is my code: 这是我的代码:

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);
        }

when running the app crashes, but the image is being saved 运行应用程序时崩溃,但是图像正在保存

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

premissions: 要求:

<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" />

What should I do? 我该怎么办?

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);
}

To open Gallery App from your code, call this 要从您的代码中打开Gallery App,请调用此

 
 
 
  
  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( "content://media/internal/images/media")); startActivity(intent);
 
  

Edit: 编辑:

To open the recently saved image 打开最近保存的图像

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);
}

All you have to do is append the image id to the end of the path for the EXTERNAL_CONTENT_URI . 您所要做的就是将图像ID附加到EXTERNAL_CONTENT_URI的路径末尾。 Then launch an Intent with the View action, and the Uri. 然后使用“视图”操作和Uri启动一个Intent。

The image id comes from querying the content resolver. 图像ID来自查询内容解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM