簡體   English   中英

為什么我無法從Android圖庫中的相機相冊中獲取圖像?

[英]Why can't I get an image from the camera album in Android gallery?

我可以從圖庫中獲取圖像,但不能從相冊/膠卷中獲取圖像。 但是我知道我從相機膠卷中獲得了圖像的URI,但它仍然不會顯示在我的imageview中。

打開畫廊:

if(resultCode == RESULT_OK){
        imgUri = data.getData();
        if(requestCode == PICK_IMAGE){
            Intent sendpic = new Intent(getApplicationContext(), GalleryChoice.class);
            sendpic.putExtra("imagePath", imgUri.toString());
            Log.d("SHOW UP", imgUri.toString());
            startActivity(sendpic);

從上一個活動中獲取URI以顯示在未顯示圖像的ImageView中。

if(getIntent().hasExtra("imagePath")){
        ur = getIntent().getStringExtra("imagePath");
        Uri newUri = Uri.parse(ur);
        try {
            bitmapy = MediaStore.Images.Media.getBitmap(this.getContentResolver(), newUri);
            imgView1.setImageBitmap(bitmapy);
        }
        catch (IOException e){

        }
  }

通常,當Image大小為2048*2048時,會發生這種情況。 通常,相機膠卷會捕獲尺寸較大的圖像,如果圖像的長度和寬度大於2048 ,則需要裁切。

  • 從相機膠卷在ImageView < 2048*2048上設置的有效尺寸

下面僅是裁剪圖像的示例(如果尺寸過大且不考慮圖像比例)

    public class InputImageCompressor extends Activity{

    public static void compressInputImage(Uri inputImageData, Context context, ImageView newIV)
    {
        Bitmap bitmap;
        //Uri inputImageData = data.getData();
        try
        {
            Bitmap bitmapInputImage = MediaStore.Images.Media.getBitmap(context.getContentResolver(), inputImageData);
            if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() > 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
                newIV.setImageBitmap(bitmap);
            }
            else if (bitmapInputImage.getWidth() > 2048 && bitmapInputImage.getHeight() < 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1920, 1200, true);
                newIV.setImageBitmap(bitmap);
            }
            else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() > 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, 1024, 1280, true);
                newIV.setImageBitmap(bitmap);
            }
            else if (bitmapInputImage.getWidth() < 2048 && bitmapInputImage.getHeight() < 2048)
            {
                bitmap = Bitmap.createScaledBitmap(bitmapInputImage, bitmapInputImage.getWidth(), bitmapInputImage.getHeight(), true);
                newIV.setImageBitmap(bitmap);
            }
        } catch (Exception e)
        {
            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
        }
    }
}

在您的代碼中使用它:

if(getIntent().hasExtra("imagePath")){
        ur = getIntent().getStringExtra("imagePath");
        Uri newUri = Uri.parse(ur);
        try {
            InputImageCompressor.compressInputImage(newUri, your_class_name.this, imgView1);
            }
        catch (IOException e){

        }
  }

為什么圖像尺寸限制為2048 * 2048 閱讀更多

檢查Uri是否有效。 如果是這樣,請使用下面的方法獲取它的真實地址。

public String getRealPathFromURI(Uri uri){
    String filePath = "";
    String[] filePahColumn = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(uri, filePahColumn, null, null, null);
    if (cursor != null) {
        if(cursor.moveToFirst()){
            int columnIndex = cursor.getColumnIndex(filePahColumn[0]);
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
    }
    return filePath;
}

使用此文件filePath設置圖像

yourImageView.setImageBitmap(BitmapFactory.decodeFile(filePath));

您可以使用它來縮小圖像。

暫無
暫無

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

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