图片视图未显示来自图库的图片。大图片/小图片均无效。
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 :(得分: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);
}