0
我想從相冊中獲取圖像,但是當我選取任何圖片時,它不會顯示並停止。我用我的手機測試,我認爲問題可能是我用我的代碼設置了錯誤的路徑。但我找不到它在哪裏。 案例100:採取圖片行動,它可以工作,案例101:挑選圖片行動。從路徑獲取圖像
這是我的onActivityResult
protected void **onActivityResult**(int requestCode , int resultCode , Intent
data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode)
{
case 100:
Intent it = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
sendBroadcast(it);
break;
case 101:
imgUri = convertUri(data.getData());
break;
}
showImg();
}
else {
Toast.makeText(this , requestCode==100? "no take the pic" : "no choose the pic" , Toast.LENGTH_LONG).show();
}
}
這是我converUri
Uri convertUri(Uri uri){
if(uri.toString().substring(0,7).equals("content"))
{
String[] colName = {MediaStore.MediaColumns.DATA};
Cursor cursor = getContentResolver().query(uri,colName,null,null,null);
cursor.moveToFirst();
uri = Uri.parse("file://"+ cursor.getString(0));
}
return uri;
}
這是我showImag
void showImg(){
imv = (ImageView) findViewById(R.id.imageView);
int iw ,ih , vw, vh;
boolean needRotate;
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgUri.getPath(),option);
iw = option.outWidth;
ih = option.outHeight;
vw = imv.getWidth();
vh = imv.getHeight();
int scaleFactor;
if(iw<ih)
{
needRotate = false;
scaleFactor = Math.min(iw/vw , ih/vh);
}else{
needRotate = true ;
scaleFactor =Math.min(ih/vw , iw/vh);
}
option.inJustDecodeBounds = false;
option.inSampleSize = scaleFactor ;
Bitmap bmp = BitmapFactory.decodeFile(imgUri.getPath(),option);
if(needRotate)
{
Matrix matrix = new Matrix();
matrix.postRotate(90);
bmp = Bitmap.createBitmap(bmp , 0 , 0 ,bmp.getWidth(), bmp.getHeight(), matrix , true);
}
imv.setImageBitmap(bmp);
}
你的答案,但它不能工作。當我選擇時,它停止。是否我的convertUri或我的手機或使用權限有問題。 –