2014-02-20 80 views
0

圖片視圖不能顯示圖片來自gallery.Large/small圖像都不起作用。ImageView不顯示圖片取自Android媒體/多媒體

private static final int RESULT_LOAD_IMAGE = 0; 

public void GalleryAction(View v){ 

     Intent galleryintent=new Intent(); 
     galleryintent.setType("image/*"); 
     galleryintent.setAction("android.intent.action.GET_CONTENT"); 
     startActivityForResult(galleryintent, RESULT_LOAD_IMAGE); 

    } 

Uri resultFile; 
int width; 
int height; 
Bitmap bm,bmtamp; 
String pathString ; 
Matrix matrixImage1; 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


     // TODO Auto-generated method stub 
    if(requestCode==RESULT_LOAD_IMAGE){ 
     try{ 

      this.resultFile = data.getData(); 
      String[] arrayOfString = { MediaStore.Images.Media.DATA }; 
      Cursor localCursor = getContentResolver().query(SavingFrameActivity.outputFileUri, arrayOfString, null, null, null); 

      localCursor.moveToFirst(); 
      int columnIndex = localCursor.getColumnIndex(arrayOfString[0]); 
      this.pathString = localCursor.getString(columnIndex); 

      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeResource(getResources(), R.id.photoImage, options); 
      int imageHeight = options.outHeight; 
      int imageWidth = options.outWidth; 
      String imageType = options.outMimeType; 

      this.imgview=(ImageView)findViewById(R.id.photoImage); 
      this.width=this.imgview.getWidth(); 
      this.height=this.imgview.getHeight(); 
this.bm=decodeSampledBitmapFromResource(getResources(), R.id.photoImage, 100, 100); 
//this.bm = decodeSampledBitmapFromPath(this.pathString, this.imgview); 
     this.imgview.setOnTouchListener(new MyonTouchListener()); 

     if (this.bm != null) 
     { 
     this.imgview.setImageBitmap(this.bm); 
     this.matrixImage1 = new Matrix(); 
     this.imgview.setImageMatrix(this.matrixImage1); 
     } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 


    } 
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
      int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 


    public static int calculateInSampleSize(BitmapFactory.Options paramOptions, int paramInt1, int paramInt2) 
    { 
     int i = paramOptions.outHeight; 
     int j = paramOptions.outWidth; 
     int k = 1; 
     int n; 
     if ((i > paramInt2) || (j > paramInt1)) 
     { 
     int m = Math.round(i/paramInt2); 
     n = Math.round(j/paramInt1); 
     if (m < n) 
      k = m; 
     } 
     else 
     { 
     return k; 
     } 
     return n; 
    } 

logcat的報告:

02-20 15:06:12.799: W/System.err(23706): java.lang.NullPointerException 
02-20 15:06:12.815: W/System.err(23706): at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1094) 
02-20 15:06:12.815: W/System.err(23706): at android.content.ContentResolver.query(ContentResolver.java:354) 
02-20 15:06:12.815: W/System.err(23706): at android.content.ContentResolver.query(ContentResolver.java:313) 
02-20 15:06:12.815: W/System.err(23706): at com.krushnanlabs.savingframe.SavingFrameActivity.onActivityResult(SavingFrameActivity.java:108) 
02-20 15:06:12.815: W/System.err(23706): at android.app.Activity.dispatchActivityResult(Activity.java:5347) 
02-20 15:06:12.815: W/System.err(23706): at android.app.ActivityThread.deliverResults(ActivityThread.java:3175) 
02-20 15:06:12.815: W/System.err(23706): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222) 
02-20 15:06:12.815: W/System.err(23706): at android.app.ActivityThread.access$1100(ActivityThread.java:140) 
02-20 15:06:12.815: W/System.err(23706): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276) 
02-20 15:06:12.815: W/System.err(23706): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-20 15:06:12.815: W/System.err(23706): at android.os.Looper.loop(Looper.java:137) 
02-20 15:06:12.815: W/System.err(23706): at android.app.ActivityThread.main(ActivityThread.java:4895) 
02-20 15:06:12.815: W/System.err(23706): at java.lang.reflect.Method.invokeNative(Native Method) 
02-20 15:06:12.815: W/System.err(23706): at java.lang.reflect.Method.invoke(Method.java:511) 
02-20 15:06:12.815: W/System.err(23706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 
02-20 15:06:12.815: W/System.err(23706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 
02-20 15:06:12.815: W/System.err(23706): at dalvik.system.NativeStart.main(Native Method) 
+0

SavingFrameActivity.java:108這到底是怎麼在這行 – Triode

回答

1

嘗試,我用同樣的來回需要這方面的工作代碼

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE); 

    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      if (requestCode == SELECT_PICTURE) { 
       Uri selectedImageUri = data.getData(); 
       selectedImagePath = getPath(selectedImageUri); 
       System.out.println("Image Path : " + selectedImagePath); 
       img.setImageURI(selectedImageUri); 
      } 
     } 
    } 

    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); 
    } 
+0

感謝@Anil,你代碼working.But我使用相同的代碼,我通過調用decodeSampledBitmapFromResourcefrom從其他類的上面寫的。它現在工作。 – Crishnan