【发布时间】:2012-05-30 16:59:16
【问题描述】:
我在从我的图库中选择一张图片并在我的应用程序中使用它时遇到了一些问题。 我使用这个意图:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_PHOTO);
这把我带到了画廊,在那里我可以挑选一张图片。
但是当我选择一个时,onActivityResult 会立即被调用,
而 ACTION_PICK 还没有完成一切。嗯,我想这就是问题所在。
我读过我应该尝试将清单中活动的launchMode 更改为singleTop,但这不起作用。或者我应该更改“ACTION_PICK”活动的launchMode?
这甚至可能吗?
final static int ACTIVITY_SELECT_PHOTO = 0;
final static int ACTIVITY_CAMERA_PHOTO = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Log.d("onActivityresult", "req = " + requestCode + ", result = " + resultCode);
switch(requestCode) {
case ACTIVITY_SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImageUri=imageReturnedIntent.getData();
String actualPath=getRealPathFromURI(selectedImageUri);
File file=new File(actualPath);
Bitmap b=decodeFile(file); //this is new bitmap which you can use for your purpose
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 55, os);
compressedByteArray = os.toByteArray();
iv.setImageBitmap(b);
currentUser.put("picture", compressedByteArray);
Toast t = Toast.makeText(this, "Uploading picture...", Toast.LENGTH_LONG);
t.show();
currentUser.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
case ACTIVITY_CAMERA_PHOTO:
if(resultCode == RESULT_OK){
Bundle extras = imageReturnedIntent.getExtras();
//THE LINE BELOW THIS ONE GIVES NULLPOINTEREX WHICH IS OBVIOUS BECAUSE IT's ANOTHER //CASE
Bitmap bmp = (Bitmap)extras.get("data");
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 55, os);
compressedByteArray = os.toByteArray();
bmp = BitmapFactory.decodeByteArray(compressedByteArray, 0, compressedByteArray.length);
iv.setImageBitmap(bmp);
currentUser.put("picture", compressedByteArray);
}
try {
Toast t = Toast.makeText(this, "Uploading picture...", Toast.LENGTH_LONG);
t.show();
currentUser.save();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ResultCode = -1(所以 RESULT_OK) 但它给了我一个错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/173 }} to activity {com.tapazz/com.tapazz.ShowProfileActivity}: java.lang.NullPointerException
nullPointerException 来自不同的 RequestCode(参见上面的“这条线”)
应用程序停止,但当我重新启动它时,我看到它已按照应有的方式选择照片
【问题讨论】:
标签: android android-activity android-gallery android-photos