【问题标题】:Android: how to open image from gallery?Android:如何从图库中打开图像?
【发布时间】:2014-09-29 20:16:39
【问题描述】:

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

这是我的代码:

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

我该怎么办?

【问题讨论】:

    标签: android image android-intent gallery actionview


    【解决方案1】:

    要从您的代码中打开 Gallery App,请调用此代码

    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 动作和 Uri 启动一个 Intent。

    图片id来自查询内容解析器。

    【讨论】:

    • 它对我有什么帮助?我想打开我刚刚创建的图像 - 不是整个画廊...
    【解决方案2】:
    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?我不想显示我的图像,我想将其作为图库的一部分打开(意图用于图库应用)
    • 对不起,我的错……我读你的问题太快了。我提供的示例用于检索从图库中选择的图像。
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多