簡體   English   中英

Android:如何從圖庫中打開圖像?

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

我在應用程序中創建了一個方法,該方法將布局保存為圖像並將其顯示在圖庫中。 我正在嘗試打開它,但是我在使用所有教程和不同方法時都遇到了困難。

這是我的代碼:

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

要從您的代碼中打開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的路徑末尾。 然后使用“視圖”操作和Uri啟動一個Intent。

圖像ID來自查詢內容解析器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM