1

在我的应用程序中,我可以选择在 sd 卡中显示图像,用户可以从中选择一个图像,它将加载到我下一个活动的图像视图中。

我已经在三星 Ace 和三星 Galaxy S3 等两台设备上测试了该应用程序。

在 2.3 版本的三星 Ace 中,我可以选择图像并从 onActivity 结果中调用下一个 Activity 并将图像加载到 Imageview 中,在这里我可以执行 n 次,(我使用的其他选项是通过相机捕获并加载下一个活动,这里的过程也很好)

当我在 4.2 版的三星 Galaxy S3 中尝试相同的过程时,我第一次能够通过相机选择或捕获并将其加载到 imageview 中。当我第二次尝试应用程序崩溃时,说

E/dalvikvm-heap( 7058): Out of memory on a 31961104-byte allocation.
E/AndroidRuntime( 7058): FATAL EXCEPTION: main
E/AndroidRuntime( 7058): java.lang.OutOfMemoryError
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
E/AndroidRuntime( 7058): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
E/AndroidRuntime( 7058): at com.tribal.pages.app.UploadPhoto$ShowCapturedImage.onPostExecute(UploadPhoto.java:442)
E/AndroidRuntime( 7058): at com.tribal.pages.app.UploadPhoto$ShowCapturedImage.onPostExecute(UploadPhoto.java:1)
E/AndroidRuntime( 7058): at android.os.AsyncTask.finish(AsyncTask.java:602)
E/AndroidRuntime( 7058): at android.os.AsyncTask.access$600(AsyncTask.java:156)
E/AndroidRuntime( 7058): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
E/AndroidRuntime( 7058): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 7058): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 7058): at android.app.ActivityThread.main(ActivityThread.java:4517)
E/AndroidRuntime( 7058): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 7058): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 7058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
E/AndroidRuntime( 7058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
E/AndroidRuntime( 7058): at dalvik.system.NativeStart.main(Native Method)
E/android.os.Debug( 2098): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error
E/widget  ( 2468): [MSC_HERO_Accu]>>> SM:399 [0:0] IR : false, IPR : false
E/Launcher( 2468): Error finding setting, default accessibility to not found: accessibility_enabled
E/widget  ( 2468): [MSC_HERO_Accu]>>> SM:399 [0:0] IR : false, IPR : false

我尝试使用以下 2 种方法在 ImageView 方法 1 中加载捕获的图像或选定的图像

Bitmap  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(Appconstants._uri));
                       bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ImageView uplaod_image.setImageBitmap(bitmap);

方法二

Bitmap bitmap = BitmapFactory.decodeFile(Appconstants.f);
ImageView uplaod_image.setImageBitmap(bitmap);

但是每次应用程序崩溃时说上述错误,如何解决这个问题......

4

2 回答 2

2

您看到的行为差异可能是由于两部手机上的图像大小不同。

你可能想打电话,

upload_image.setImageDrawable(null);

在尝试加载另一个图像之前清除 imageView(或使用此处详述的方法之一),以便在后续分配之前由垃圾收集器清理和收集内存并防止Out of memory错误。

于 2012-10-19T09:59:42.180 回答
1

尝试根据设备的屏幕尺寸在将图像加载到内存之前对其进行缩放,

您可以使用 BitmapFactory 选项inJustDecodeBounds = true在将 Bitmap 解码到内存之前获取它的大小。

获取高度和宽度,

int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

然后您可以根据您的要求使用所需的图像高度和宽度对其进行缩放,

int inSampleSize = Math.min(imageWidth /reqWidth,imageHeight /reqHeight);

然后最后应用inSampleSize来缩放位图。对于更多详细信息,请在此处查看我的答案。

于 2012-10-19T10:01:52.910 回答