将图像过大造成的应用程序回归到previous活动? [英] Will the image too big caused app return to previous activity?

查看:187
本文介绍了将图像过大造成的应用程序回归到previous活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AddMoreClaims 活动,必须有一个的ImageView 按钮和保存按钮

In AddMoreClaims Activity , there has an imageView, button and save button.

当按钮被点击,就会进入 activeGallery()键,让用户选择图像从画廊。然后选定的图像将显示在的ImageView AddMoreClaims

When button is clicked, it will goes to activeGallery() and let user select image from gallery. The selected image will then display on 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 是明确的,我已经加入德codeSampledBitmapFromUri(picturePath,200,200) 到目前为止,一切工作正常。

In order to make sure the image selected placed on the imageView is clear, I have added decodeSampledBitmapFromUri(picturePath,200,200). So far everything works fine.

当保存按钮被点击时,其设定的图像返回 AddClaims 的ListView

When save button is clicked, it supposes return the image to 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();

            }
        });

但是,上述编码有时不工作。我不知道isin't因为图像选择过大,当我点击保存按钮并希望将图像恢复的ListView AddClaims ,它只是之前的 AddClaims返回到活动中。 。但是,code适用于某些选定的图像。为什么会出现这种情况?

However, the above coding does not worked sometimes. I'm not sure isin't because the image selected too big , when I click the save button and want to return the image to listView AddClaims, it just return to the activity before AddClaims. . But the code works for some selected image. Why would this happen ?

推荐答案

首先,它是preferable脱code你的位图asynchroniously,而不是在UI线程。二,不经传的意图位图。也没有必要写任何东西在SD卡上,因为图像已经在你的设备上。

First, it is preferable to decode your bitmaps asynchroniously, not on the UI thread. Second, do not pass bitmaps via intents. There is also no need to write anything on the sd card since the image is already on your device.

我建议你使用图像加载库 - 你可以看到一些最好的这里上市。

I recommend you to use an image loading library - you can see some of the best listed here.

这些图书馆的主要目的是下载,缓存和从互联网上显示图像,但他们也从本地存储显示图像工作的伟大。

Those libraries' main purpose is to download, cache and display images from the internet, but they also work great for displaying images from the local storage.

例如,如果您选择毕加索,你的code将是这样的:

For example, if you choose Picasso, your code will be something like this:

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

正如你可以看到,该库生成,庄稼和显示位图给你。

As you can see, the library generates, crops and displays the bitmap for you.

点击您的按钮,就可以通过 picturePath 通过没有图片,您可以使用相同的方法显示图像的其他活动 - Picasso.with(本)...

When your button is clicked, you can pass picturePath to your other Activity via Intet, where you can display the image using the same approach - Picasso.with(this)...

这篇关于将图像过大造成的应用程序回归到previous活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆