2016-09-22 51 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

相关问题