外部配置太大,此过程太大、过程

2023-09-04 11:57:12 作者:钢铁大侠

我发布了一个问题,昨天晚上过这个问题,但我不认为我解释的不够好得到适当的帮助。所以基本上,我有一个应用程序,您可以在preSS一个按钮,可以让你选择从您的画廊图像,并显示在我已经显示在应用程序的ImageView的。这一切的伟大工程。然而,当我preSS一次按钮,并选择不同的图片应用程序的力量关闭。

更新现在,如果我从我的下载照片文件夹中的照片在画廊工作正常,可以切换照片经常因为我喜欢。但是,当我回到我的相机照片文件夹来改变图片就强制关闭。

更新2 这也工作正常,与任何其他文件夹,我在我的画廊的应用程序。只有一个有这个问题(强制关闭)是在摄像机文件夹

更新3 19660800字节的外部分配太大,此过程。是导致该问题。被选上减少照片的大小什么办法?

 私有静态最终诠释SELECT_PICTURE = 1;

      私人字符串selectedImagePath;
      私人ImageView的IMG;

      公共无效的onCreate(包savedInstanceState){
          super.onCreate(savedInstanceState);
          的setContentView(R.layout.main);

          IMG =(ImageView的)findViewById(R.id.myimageview);

          ((按钮)findViewById(R.id.taptoadd))
                  .setOnClickListener(新OnClickListener(){
                      公共无效的onClick(查看为arg0){
                          意向意图=新的意图();
                          intent.setType(图像/ *);
                          intent.setAction(Intent.ACTION_GET_CONTENT);
                          startActivityForResult(Intent.createChooser(意向,选择图片),SELECT_PICTURE);
                      }
                  });
      }

      公共无效onActivityResult(INT申请code,INT结果code,意图数据){
          如果(结果code == RESULT_OK){
              如果(要求code == SELECT_PICTURE){
                  乌里selectedImageUri = data.getData();
                  selectedImagePath = getPath(selectedImageUri);
                  的System.out.println(映像路径:+ selectedImagePath);
                  img.setImageURI(selectedImageUri);
              }
         }
      }

      公共字符串getPath(URI URI){
         的String []投影= {MediaStore.Images.Media.DATA};

          光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
          INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          cursor.moveToFirst();
          返回cursor.getString(Column_Index中);


      }
 

下面是日志

  11-23 16:33:15.871:D / dalvikvm(9171):GC_EXTERNAL_ALLOC释放51K,49%免费2793K / 5447K,外部20868K / 22916K,暂停50毫秒
    11-23 16:33:15.878:E / dalvikvm堆(9171):19660800字节外部分配太大,此过程。
    11-23 16:33:15.886:E / GraphicsJNI(9171):虚拟机不会让我们分配19660800字节
    11-23 16:33:15.906:D / dalvikvm(9171):GC_FOR_MALLOC释放< 1K,49%免费2792K / 5447K,外部1668K / 20868K,暂停18毫秒
    11-23 16:33:15.906:D / Skia的(9171):---德codeR->德code返回false
    11-23 16:33:15.906:D / AndroidRuntime(9171):关闭虚拟机
    11-23 16:33:15.910:W / dalvikvm(9171):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
    11-23 16:33:15.937:E / AndroidRuntime(9171):致命异常:主要
    11-23 16:33:15.937:E / AndroidRuntime(9171):java.lang.OutOfMemoryError:位图大小超过VM预算
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.graphics.BitmapFactory.nativeDe codeStream(本机方法)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:470)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.graphics.BitmapFactory.de codeResourceStream(BitmapFactory.java:336)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.widget.ImageView.resolveUri(ImageView.java:521)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.widget.ImageView.setImageURI(ImageView.java:305)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在snapshot.app.LinearLayoutsActivity.onActivityResult(LinearLayoutsActivity.java:48)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.app.Activity.dispatchActivityResult(Activity.java:3908)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.app.ActivityThread.access $ 2000(ActivityThread.java:117)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:961)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.os.Handler.dispatchMessage(Handler.java:99)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.os.Looper.loop(Looper.java:130)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在android.app.ActivityThread.main(ActivityThread.java:3683)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在java.lang.reflect.Method.invokeNative(本机方法)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在java.lang.reflect.Method.invoke(Method.java:507)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    11-23 16:33:15.937:E / AndroidRuntime(9171):在dalvik.system.NativeStart.main(本机方法)
 

解决方案 JDK配置JAVAC不是内部或外部命令解决方法

您也越来越意味着要装入的位图太大的错误。您需要下采样它。关于如何处理这方面的一些很好的例子,看到这一问题:Android:奇怪的内存不足问题

更新:不是告诉这些URI加载一个位图ImageView的,你需要先载入位图自己,然后设置位图作为对ImageView的图像。这是pretty的简单。

要加载从您的位图必须首先转换的URI一个字符串(我想你可以使用 URI.getPath()虽然我不知道),其中包含路径到图像,然后你可以载入位图:

 位图B = BitmapFactory.de codeFILE(映像路径);
 

这将取消code。整个位图,而不是要下采样位图所需的分辨率。比方说你的ImageView会为125x125像素,图像是1000×1000像素。 千分之一百二十五= 8 所以我们只需要脱code每8个像素。要做到这一点,我们做到以下几点:

  INT inSample = 8;

OPTS =新BitmapFactory.Options();
opts.inSampleSize = inSample;

位图B = BitmapFactory.de codeFILE(映像路径,选择采用); //这个位图将是1/8原来的大小
 

其pretty的直线前进,并应解决您的内存问题。见我的回答的第二部分对这个问题:的Andr​​oid GalleryView回收了解更详尽的解释

I posted a question last night about this issue, but I dont think I explained it well enough to get the proper help. So basically, I have an application where you can press a button which will let you select an image from your gallery and show it in an ImageView I have displayed in the app. This all works great. However, when I press the button again and choose a different picture the app force closes.

UPDATE Now, if I take a photo from my downloads photo folder in the gallery it works fine, can switch photos as often as I like. But when I go back to my camera photo folder to change the picture it force closes.

UPDATE 2 It also works fine with any other folder I have in my Gallery app. Only one having this problem (force close) is on the "Camera" Folder

UPDATE 3 19660800-byte external allocation too large for this process. is causing the issue. Any ways on reducing the size of the photos being selected?

        private static final int SELECT_PICTURE = 1;

      private String selectedImagePath;
      private ImageView img;

      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

          img = (ImageView)findViewById(R.id.myimageview);

          ((Button) findViewById(R.id.taptoadd))
                  .setOnClickListener(new OnClickListener() {
                      public void onClick(View arg0) {
                          Intent intent = new Intent();
                          intent.setType("image/*");
                          intent.setAction(Intent.ACTION_GET_CONTENT);
                          startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                      }
                  });
      }

      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode == RESULT_OK) {
              if (requestCode == SELECT_PICTURE) {
                  Uri selectedImageUri = data.getData();
                  selectedImagePath = getPath(selectedImageUri);
                  System.out.println("Image Path : " + selectedImagePath);
                  img.setImageURI(selectedImageUri);
              }
         }
      }

      public String getPath(Uri uri) {
         String[] projection = { MediaStore.Images.Media.DATA };

          Cursor cursor = managedQuery(uri, projection, null, null, null);
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          cursor.moveToFirst();
          return cursor.getString(column_index);


      }

here is the log

              11-23 16:33:15.871: D/dalvikvm(9171): GC_EXTERNAL_ALLOC freed 51K, 49% free 2793K/5447K, external 20868K/22916K, paused 50ms
    11-23 16:33:15.878: E/dalvikvm-heap(9171): 19660800-byte external allocation too large for this process.
    11-23 16:33:15.886: E/GraphicsJNI(9171): VM won't let us allocate 19660800 bytes
    11-23 16:33:15.906: D/dalvikvm(9171): GC_FOR_MALLOC freed <1K, 49% free 2792K/5447K, external 1668K/20868K, paused 18ms
    11-23 16:33:15.906: D/skia(9171): --- decoder->decode returned false
    11-23 16:33:15.906: D/AndroidRuntime(9171): Shutting down VM
    11-23 16:33:15.910: W/dalvikvm(9171): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    11-23 16:33:15.937: E/AndroidRuntime(9171): FATAL EXCEPTION: main
    11-23 16:33:15.937: E/AndroidRuntime(9171): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.widget.ImageView.resolveUri(ImageView.java:521)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.widget.ImageView.setImageURI(ImageView.java:305)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at snapshot.app.LinearLayoutsActivity.onActivityResult(LinearLayoutsActivity.java:48)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.os.Handler.dispatchMessage(Handler.java:99)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.os.Looper.loop(Looper.java:130)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at android.app.ActivityThread.main(ActivityThread.java:3683)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at java.lang.reflect.Method.invoke(Method.java:507)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    11-23 16:33:15.937: E/AndroidRuntime(9171):     at dalvik.system.NativeStart.main(Native Method)

解决方案

The error you are getting means that the bitmap you are loading is too big. You need to downsample it. See this question for some great examples on how to deal with this: Android: Strange out of memory issue

Update: Instead of telling the ImageView which URI to load as a Bitmap, you need to load the bitmap first yourself and then set that bitmap as the image on the ImageView. This is pretty straightforward.

To load a bitmap from you must first convert the URI to a String (I think you can use URI.getPath() although I'm not sure) that contains the path to the image then you can load the bitmap with:

Bitmap b = BitmapFactory.decodeFile(pathToImage);

This will decode the entire bitmap, instead you want to downsample the bitmap to the desired resolution. Lets say your imageview will be 125x125 pixels and your image is 1000x1000 pixels. 125/1000 = 8 so we only need to decode every 8th pixel. To do this we do the following:

int inSample = 8;

opts = new BitmapFactory.Options();
opts.inSampleSize = inSample;

Bitmap b = BitmapFactory.decodeFile(pathToImage, opts); // this bitmap will be 1/8 the size of the original

Its pretty straight forward and should solve your memory issues. See the 2nd part of my answer to this question: Android GalleryView Recycling for a more extensive explanation.