相关疑难解决方法(0)

将图像加载到Bitmap对象时出现奇怪的内存不足问题

我有一个列表视图,每行有几个图像按钮.单击列表行时,将启动新活动.由于相机布局有问题,我不得不建立自己的标签.为结果启动的活动是地图.如果我单击我的按钮启动图像预览(从SD卡加载图像),应用程序将从活动返回到活动返回到listview结果处理程序以重新启动我的新活动,这只是一个图像小部件.

列表视图上的图像预览正在使用光标和ListAdapter.这使得它非常简单,但我不确定如何放置一个经过调整大小的图像(即小的像素大小不像动态src图像按钮那样.所以我只是调整了从手机摄像头下来的图像.

问题是当我试图返回并重新启动第二个活动时,我收到内存不足错误.

  • 有没有办法我可以轻松地逐行构建列表适配器,我可以在运行中调整大小(有点明智)?

这是更好的,因为我还需要对每行中的小部件/元素的属性进行一些更改,因为焦点问题我无法选择带触摸屏的行.(我可以用滚球.)

  • 我知道我可以做一个带外调整大小并保存我的图像,但这不是我想要做的,但是一些示例代码会很好.

一旦我在列表视图上禁用了图像,它再次正常工作.

仅供参考:这就是我的做法:

String[] from = new String[] { DBHelper.KEY_BUSINESSNAME,DBHelper.KEY_ADDRESS,DBHelper.KEY_CITY,DBHelper.KEY_GPSLONG,DBHelper.KEY_GPSLAT,DBHelper.KEY_IMAGEFILENAME  + ""};
int[] to = new int[] {R.id.businessname,R.id.address,R.id.city,R.id.gpslong,R.id.gpslat,R.id.imagefilename };
notes = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
Run Code Online (Sandbox Code Playgroud)

哪里R.id.imagefilenameButtonImage.

这是我的LogCat:

01-25 05:05:49.877: ERROR/dalvikvm-heap(3896): 6291456-byte external allocation too large for this process.
01-25 05:05:49.877: ERROR/(3896): VM wont let us allocate 6291456 bytes
01-25 05:05:49.877: ERROR/AndroidRuntime(3896): Uncaught handler: thread main exiting due to …
Run Code Online (Sandbox Code Playgroud)

android image bitmap out-of-memory android-bitmap

1252
推荐指数
33
解决办法
60万
查看次数

BitmapFactory.decodeStream在设置选项时返回null

我有问题BitmapFactory.decodeStream(inputStream).在没有选项的情况下使用它时,它将返回一个图像.但是当我将它与选项一起使用时,.decodeStream(inputStream, null, options)它永远不会返回Bitmaps.

我要做的是在实际加载之前对Bitmap进行下采样以节省内存.我读过一些很好的指南,但都没有使用.decodeStream.

工作很精细

URL url = new URL(sUrl);
HttpURLConnection connection  = (HttpURLConnection) url.openConnection();

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);
Run Code Online (Sandbox Code Playgroud)

什么都不行

InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is, null, options);

InputStream is = connection.getInputStream();

Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeStream(is, null, options);

Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH);

if (options.outHeight * options.outWidth * 2 >= 200*100*2){
    // Load, …
Run Code Online (Sandbox Code Playgroud)

java android image bitmap

87
推荐指数
2
解决办法
6万
查看次数

在缩放加载之前,从实际Uri预先猜测Bitmap的大小

如果你有一个Uri并且你需要它Bitmap,理论上你可以做到这一点

Bitmap troublinglyLargeBmp =
  MediaStore.Images.Media.getBitmap(
      State.mainActivity.getContentResolver(), theUri );
Run Code Online (Sandbox Code Playgroud)

但它每次都会崩溃,

所以你这样做.........

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;

AssetFileDescriptor fileDescriptor =null;
fileDescriptor =
  State.mainActivity.getContentResolver().openAssetFileDescriptor( theUri, "r");

Bitmap actuallyUsableBitmap
  = BitmapFactory.decodeFileDescriptor(
    fileDescriptor.getFileDescriptor(), null, options);

Utils.Log("'4-sample' method bitmap ... "
   +actuallyUsableBitmap.getWidth() +" "
   +actuallyUsableBitmap.getHeight() );
Run Code Online (Sandbox Code Playgroud)

太棒了,是工作的解决方案.

请注意,在当前条件下(2014年),典型的相机尺寸等,"四"因子往往效果很好.但是,最好在Uri猜测或准确了解图像数据的大小,然后使用它信息,智能地选择那个因素.

简而言之,如何在加载Uri时正确选择该比例因子

Android专家,这是一个众所周知的问题,有没有解决方案?感谢您的iOS-> Android好友!:)

android android-camera bitmapfactory

14
推荐指数
1
解决办法
5994
查看次数

在ViewPager中将大位图加载到ImageView - 内存不足

我有ViewPager用于显示可缩放的图像(使用ImageViewTouch).我需要从Internet(http)加载大型位图.我的意思是2000x1000.图像需要如此之大,因为它们是可缩放的,需要显示细节.服务器上的图像是.jpg格式,但不是问题 - 我可以更改它.

如何设置将如此大的图像加载到ImageViewTouch(ImageView)而不会获得带内存的问题?

到现在为止我只使用这个(AsyncTask):

    ImageView currentView; //ImageView where to place image loaded from Internet
    String ImageUrl; //URL of image to load

    protected Bitmap doInBackground(ArrayList... params) {

            Bitmap bitmap;
            InputStream in = null;
            imageUrl = (String)params[0].get(1);

            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(new HttpGet(imageUrl));
                in = response.getEntity().getContent();
            } catch(Exception e){
                e.printStackTrace();
            }
            try {
                bitmapa = BitmapFactory.decodeStream(in);
                in.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            return bitmap;
        }


        @Override
        protected void onPostExecute(Bitmap bitmap) {

            currentView.setImageBitmap(bitmap); …
Run Code Online (Sandbox Code Playgroud)

android bitmap out-of-memory imageview bitmapfactory

6
推荐指数
1
解决办法
5036
查看次数

android位图中的内存不足错误

我正在使用位图.它会抛出内存错误(5次中有2次).怎么可以避免.

以下是我的代码:

  bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
  photo_new= rotateImage(bitmap, 90);
  ByteArrayOutputStream stream = new ByteArrayOutputStream();

  photo_new.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  byte[] byteArray = stream.toByteArray();

  Intent i = new Intent(getApplicationContext(),new_class.class);
  i.putExtra("image", byteArray);

  startActivity(i);
  byteArray=null;
Run Code Online (Sandbox Code Playgroud)

java android bitmap out-of-memory

3
推荐指数
1
解决办法
3946
查看次数