2016-09-22 18 views
0

我可以從圖庫中獲取圖片,但是我無法從相冊/卷中獲得。但是我知道我從相機膠捲獲得了圖像的URI,但它仍然不會顯示在我的圖像視圖中。爲什麼我無法從Android圖庫中的相機相冊中獲取圖像?

打開圖庫:

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){ 

     } 
    } 

回答

0

它一般發生在當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 read more

0

檢查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)); 

你可以用它來縮小圖像。

+0

我的回答有幫助嗎? – W4R10CK

相關問題