我在从我的图库中选择图像并在我的应用程序中使用它时遇到了一些问题。我使用这个意图:
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
但这没有用。或者我应该改变launchMode
“ACTION_PICK”活动的?这甚至可能吗?
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(参见上面的“这条线”)
该应用程序停止,但是当我重新启动它时,我看到它已经像它应该做的那样选择了照片