预期的图像但返回null值

时间:2012-05-17 05:45:23

标签: android

我正在尝试从我的图库中加载图像文件,但是我得到的是null而不是预期的图像。这是我正在使用的代码:

public static Bitmap decodeSampledBitmapFromStreem(InputStream is,
        int reqWidth, int reqHeight) {
    Bitmap b = null;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    b = BitmapFactory.decodeStream(is, null, options);
    return b;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

我能在代码中看到任何问题吗?为什么我会变空?

请注意,如果我对第6行发表评论,我的代码适用于小图片,但是根据我的错误记录,我收到大图像错误:

05-17 11:37:46.277: E/AndroidRuntime(9224): FATAL EXCEPTION: AsyncTask #3
05-17 11:37:46.277: E/AndroidRuntime(9224): java.lang.RuntimeException: An error occured while executing doInBackground()
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.lang.Thread.run(Thread.java:1019)
05-17 11:37:46.277: E/AndroidRuntime(9224): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at com.giftcard.GiftCard.decodeSampledBitmapFromStreem(GiftCard.java:454)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at com.giftcard.GiftCard$MyBitmapDecoder.doInBackground(GiftCard.java:500)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at com.giftcard.GiftCard$MyBitmapDecoder.doInBackground(GiftCard.java:1)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-17 11:37:46.277: E/AndroidRuntime(9224):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-17 11:37:46.277: E/AndroidRuntime(9224):     ... 4 more
05-17 11:37:49.387: I/Process(9224): Sending signal. PID: 9224 SIG: 9

4 个答案:

答案 0 :(得分:1)

可能是因为多次使用isnputStream尝试避免首次使用 BitmapFactory.decodeStream(is,null,options);

public static Bitmap decodeSampledBitmapFromStreem(InputStream is,
        int reqWidth, int reqHeight) {
    Bitmap b = null;
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    **BitmapFactory.decodeStream(is, null, options);**
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    options.inJustDecodeBounds = false;
    b = BitmapFactory.decodeStream(is, null, options);
    return b;
}

答案 1 :(得分:0)

这是内存泄漏的问题。

你会在这里找到解决方案:

Strange out of memory issue while loading an image to a Bitmap object

答案 2 :(得分:0)

要从图库中获取图像请尝试以下代码

public class SelectPhotoFromGallery extends Activity 
{

private static final int SELECT_PICTURE = 1;
private String selectedImagePath="";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  intent.addCategory(Intent.CATEGORY_OPENABLE);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivityForResult(intent, SELECT_PICTURE); 
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE)
        {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

        File imgFile = new  File(selectedImagePath);
 if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);

 }}}}}

答案 3 :(得分:0)

您是否尝试过使用decodeByteArray?