2016-05-19 63 views
-1

我想在ImageView的選擇從SD卡,並顯示用戶照片安卓:從SD卡setImageBitmap不行

我的代碼:

try { 
    bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
} 
imgOne.setImageBitmap(bm); 

但是這個代碼不工作

此代碼爲onActivityResult選擇用戶後照片

我的課程:

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == Activity.RESULT_OK) { 
      Log.i("error", "" + requestCode); 
      final ImageView imgOne = (ImageView) G.currentActivity.findViewById(R.id.imgOne); 
      if (requestCode == 1) { 
       Bitmap bm = null; 
       if (data != null) { 
        try { 
         bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 
         //ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
         //bm.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

         //bm = Bitmap.createScaledBitmap(bm, 600, 600, true); 
        } 
        catch (IOException e) { 
         e.printStackTrace(); 
        } 

        imgOne.setImageBitmap(bm); 
        Log.i("error", "" + bm); 
       } 
      } 
     } 
    } 
+0

是你用意向選擇圖像? –

+0

是的,這個代碼經過用戶選擇並保存到數據 – Mohammad

+0

可以發佈您的onActivityResult代碼 –

回答

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

     if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) { 

     final Uri imageUri = data.getData(); 
       final InputStream imageStream = getContentResolver().openInputStream(imageUri); 
       final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); 

       imageView.setImageBitmap(selectedImage); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
+0

不工作這段代碼:( – Mohammad

+0

@Mohammad試試這個 –