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

标签 android image android-intent gallery actionview

我在我的应用程序中创建了一个方法,将我的布局保存为图像并将其显示在图库中。 我正在尝试打开它,但我很难理解所有教程和不同的方法...

这是我的代码:

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

我该怎么办?

最佳答案

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

关于Android:如何从图库中打开图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25168116/

相关文章:

jquery - 视差网站 - Img 在我滚动之前不会出现

java - 背景图像隐藏所有 GUI 设计组件

android - ActivityResultContracts 使用户在画廊或相机之间选择上传图片的方式

android - 如何以编程方式接连调用3个电话?

Android开启WiFi调试后找不到附属设备

java - Android 应用程序上的 POST 和 GET 难题

android - react native 。 View 的样式不起作用,我该如何解决?

android - 拍摄 Android 手机的打印屏幕?

android - 在 native react 中将两个图像组合成一个新图像

android - 为什么 PendingIntent 不发回我的 Intent 自定义 Extras 设置?