繁体   English   中英

Android-位图分辨率损失

[英]Android - bitmap resolution loss

与从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);

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);
            }
        }
};

它应该为您工作。 让我知道是否需要其他帮助。

在您的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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM