图片太大会导致应用返回之前的活动吗?

时间:2015-12-16 15:03:59

标签: android image listview camera

AddMoreClaims 活动中,有imageViewbutton并保存button

单击按钮时,它将转到activeGallery()并让用户从图库中选择图像。然后,所选图片将显示在imageView AddMoreClaims 上。

  private void activeGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver()
                            .query(selectedImage, filePathColumn, null, null,
                                    null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    photo = decodeSampledBitmapFromUri(picturePath,200,200); // make image clear
                    imageView.setImageBitmap(photo); // display image on imageView
                }
                break;
}

 public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
                                             int reqHeight) {

        Bitmap bm = null;
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(path, options);
        return bm;
    }

    public int calculateInSampleSize(

            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2
            // and keeps both height and width larger than the requested
            // height and width.
            while ((halfHeight / inSampleSize) > reqHeight &&
                    (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }
}

为了确保放置在imageView上的图片清晰,我添加了decodeSampledBitmapFromUri(picturePath,200,200)到目前为止一切正常。

单击“保存”按钮时,它会将图像返回到 AddClaims listView

 saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent(); // back to AddClaims
                returnIntent.putExtra("photo", photo);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();

            }
        });

但是,上述编码有时不起作用。我不确定是不是因为选择的图片太大,当我点击保存按钮并希望将图片返回到listView AddClaims 时,它只返回到之前的活动< strong> AddClaims。。但该代码适用于某些选定的图像。为什么会这样?

4 个答案:

答案 0 :(得分:2)

这不是通过返回意图传递图像的好方法。尝试将其写入SD卡,从其他屏幕访问。(如果保密则删除它)

图像位图的大小范围通常为18MB + ...因此,根据堆的可用性,它可能会也可能不起作用。 即使在高端设备中,这也可能由于堆中空间不可用而发生。或者,您可以在执行此操作之前以编程方式从堆中清除不需要的项目。

答案 1 :(得分:2)

首先,最好是异步解码位图,而不是在UI线程上解码。其次,不要通过意图传递位图。由于图像已经在您的设备上,因此无需在SD卡上写任何内容。

我建议你使用图片加载库 - 你可以看到一些最好的listed here

这些图书馆的主要目的是从互联网上下载,缓存和显示图像,但它们也非常适合显示来自本地存储的图像。

例如,如果您选择Picasso,您的代码将是这样的:

Picasso.with(this) 
    .load(new File(picturePath)) 
    .resize(yourTargetWidth, yourTargetHeight)
    .centerInside() 
    .into(imageView);

如您所见,库会为您生成,裁剪和显示位图。

单击按钮后,您可以通过Intet将picturePath传递到其他活动,您可以使用相同的方法显示图像 - Picasso.with(this)...

答案 2 :(得分:0)

您应该遵循这些做法以避免OOM例外:

  1. 所有位图操作都应该在工作线程上完成。 http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
  2. 处理完成后,使用位图文件路径创建位图缓存。
  3. 通过意图将此位图文件路径传递给另一个活动。
  4. 在新活动中,使用提供的文件路径从缓存中获取位图。
  5. 对于位图缓存,您可以使用以下任何库:

    http://square.github.io/picasso/

    https://github.com/bumptech/glide

    https://github.com/nostra13/Android-Universal-Image-Loader

答案 3 :(得分:0)

为了玩Bitmap,我们必须尽可能地优化我们的代码。要么我们必须给它时间,要么可以使用已经过优化的图像加载库。 我在我的项目中使用了Picaso。 它将处理图像大小并返回所选图像位图。 我使用这段代码从库中获取图像,它就像魅力一样。

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_TAKE_GALLERY);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_TAKE_GALLERY) {
                    Uri selectedImage = data.getData();
                    String[] filePath = {MediaStore.Images.Media.DATA};
                    Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                    c.moveToFirst();
                    int columnIndex = c.getColumnIndex(filePath[0]);
                    String picturePath = c.getString(columnIndex);

                    c.close();
                    Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                    Log.e("path of image from gallery......******************.........", picturePath + "");

                    Picasso.with(mContext).load(new File(picturePath)).fit().centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE).into(mProfilePic);    //Here mContext is context of Screen Context mContext;         
                }
            }
        }

当您需要将所选图像发送到其他活动时。您可以查看此链接。

https://stackoverflow.com/a/11010565/2022000