2017-05-18 2 views
0

から画像を表示しない意図でゲーラリーから画像を取得コードを、私は自分のアプリケーションにギャラリーから画像を取得し、ImageViewの中にそれを入れてみましたが、私はエラーを取得する「OutOfMemoryErrorが」ギャラリー

FATAL EXCEPTION: main 
java.lang.OutOfMemoryError 
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623) 
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:695) 
at kz.app.discount.activities.AddingProduct.onActivityResult(AddingProduct.java:374) 
at android.app.Activity.dispatchActivityResult(Activity.java:5456) 
at android.app.ActivityThread.deliverResults(ActivityThread.java:3402) 
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449) 
at android.app.ActivityThread.access$1200(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5279) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
at dalvik.system.NativeStart.main(Native Method)` 

。 ACTION_PICKとonActivityResult():のonCreate()onCreaの

imgProfile.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent gallary = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(gallary,LOAD_IMG); 
     } 
    }); 

外部に

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 

     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

     Bitmap bitmap = null; 
     ImageView imageView = iVAddProductDiscountPicture; 

     switch (requestCode) { 
      case GALLERY_REQUEST: 
       if (resultCode == RESULT_OK) { 
        Uri selectedImage = imageReturnedIntent.getData(); 
        try { 
         bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage); 
         final ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
         bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream); 
         Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(stream.toByteArray())); 
         //TODO: дописать код сжатия изображения 
         imageView.setImageBitmap(decoded); 

        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        alertDialog.dismiss(); 

       } 
     } 
    } 
+0

'OutOfMemoryError'は画像があなたのデバイスで処理するには大きすぎることを意味しています。既にスケールしてください。 –

+2

@MichaelDodd、ファイルサイズは関係ありません。解像度のみが問題ありません。 –

+1

@Wladimir Rudnickyビットマップ圧縮のサイズを10から100まで増やし、bitmap.compress(Bitmap.CompressFormat.JPEG、100、stream)を試してください。 –

答えて

-1

TE()

@Override 
    protected void onActivityResult(int requestCode,int resultCode,Intent data){ 
    super.onActivityResult(requestCode,resultCode,data); 

    try{ 
     if(requestCode==LOAD_IMG && resultCode==RESULT_OK && data!=null){ 

      Uri selectedImage = data.getData(); 
      String []filePath = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage,filePath,null,null,null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePath[0]); 
      String imgDecodableString = cursor.getString(columnIndex); 
      cursor.close(); 

      imgProfile.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString)); 

     }else{ 
      Toast.makeText(this, "Please select image", Toast.LENGTH_LONG).show(); 
     } 
    }catch (Exception e){ 
     Toast.makeText(this, "error", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

私はこれを試しました。イメージビューは空のイメージを取得しますが、エラーは表示されません。 –

+0

詳細については**、これがどのように 'OutOfMemoryError'に対処するかを説明してください。 – CommonsWare

0

はこれで試してみてください。

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == SELECT_PICTURE) { 
      selectedImageUri =data.getData(); 
       Log.d("uri:----",""+selectedImageUri); 
    if (null != selectedImageUri) { 
       imgView.setImageURI(selectedImageUri); 
      } 
     } 
    }