我正在尝试在我的程序中获取我的画廊图片。我可以通过使用以下代码来做同样的事情:
打开画廊:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,1);
并获取结果并显示所选图像:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode)
{
case SELECT_PHOTO:
if(resultCode == RESULT_OK)
{
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try
{
imageStream = getContentResolver().openInputStream(imageReturnedIntent.getData());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
selectedContactPicture = BitmapFactory.decodeStream(imageStream);
setContactPicture.setBackgroundDrawable(new BitmapDrawable(selectedContactPicture));
}
}
}
它第一次工作正常。但是,如果我从我的画廊中选择另一张图片,我的应用程序就会崩溃。请帮我解决这个问题。
日志猫:
05-08 10:45:12.180: E/AndroidRuntime(16782): FATAL EXCEPTION: main
05-08 10:45:12.180: E/AndroidRuntime(16782): java.lang.OutOfMemoryError
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:555)
05-08 10:45:12.180: E/AndroidRuntime(16782): at com.deepsa.apps.callfaker.MainActivity.onActivityResult(MainActivity.java:89)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.app.ActivityThread.deliverResults(ActivityThread.java:2996)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3043)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.app.ActivityThread.access$1100(ActivityThread.java:127)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.os.Looper.loop(Looper.java:137)
05-08 10:45:12.180: E/AndroidRuntime(16782): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-08 10:45:12.180: E/AndroidRuntime(16782): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:45:12.180: E/AndroidRuntime(16782): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:45:12.180: E/AndroidRuntime(16782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-08 10:45:12.180: E/AndroidRuntime(16782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-08 10:45:12.180: E/AndroidRuntime(16782): at dalvik.system.NativeStart.main(Native Method)
谢谢