Android - 位图分辨率丢失

时间:2017-03-01 19:52:12

标签: android android-intent bitmap

与来自res/drawable的位图相比,来自intent的相同位图的质量要低得多。

由于当前项目真的关注图像质量,我想从手机存储中获取原始位图(没有压缩或分辨率损失)。

那么,我怎样才能从intent获得高质量的位图?

我使用intent以这种方式从外部存储中获取位图,

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Button galleryBtn = new Button(getActivity());
    galleryBtn.setText("Import Photo...");
    galleryBtn.setOnClickListener(v -> {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        this.startActivityForResult(intent, 0);
    });

    //... some more code here
}

//... some more code here

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0)
        try {
            if (data != null) {
                InputStream stream = getActivity().getContentResolver().openInputStream(
                    data.getData());
                imageBitmap = BitmapFactory.decodeStream(stream);
                stream.close();

                imageView.setImageBitmap(imageBitmap);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
}

我以这种方式从res获取位图,

imageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);

2 个答案:

答案 0 :(得分:-1)

Intent具有传递intent的大小限制,因此您可以使用公共静态对象将位图从服务传递到广播

public class ImageBox {
    public static Queue<Bitmap> mQ = new LinkedBlockingQueue<Bitmap>(); 
}

传递服务,

private void downloadFile(final String url){
        mExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                Bitmap b = BitmapFromURL.getBitmapFromURL(url);
                synchronized (this){
                    TaskCount--;
                }
                Intent i = new Intent(ACTION_ON_GET_IMAGE);
                ImageBox.mQ.offer(b);
                sendBroadcast(i);
                if(TaskCount<=0)stopSelf();
            }
        });
}

广播接收器,

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            LOG.d(TAG, "BroadcastReceiver get broadcast");

            String action = intent.getAction();
            if (DownLoadImageService.ACTION_ON_GET_IMAGE.equals(action)) {
                Bitmap b = ImageBox.mQ.poll();
                if(b==null)return;
                if(mListener!=null)mListener.OnGetImage(b);
            }
        }
};

它应该适合你。 如果需要任何其他帮助,请告诉我。

答案 1 :(得分:-1)

在你的onActivityResult中尝试这个,

                Uri uri = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String imagePath = cursor.getString(columnIndex);
                Bitmap imageBitmap = BitmapFactory.decodeFile(imagePath);
                cursor.close();
相关问题