[英]Android - bitmap resolution loss
Compared with the bitmap getting from res/drawable
, the quality of the same bitmap getting from intent
is much lower. 与从
res/drawable
获取位图相比,从intent
获取同一位图的质量要低得多。
Since the current project is really concerned about the image quality, I want to get the original bitmaps (without compression or resolution loss) from phone storage. 由于当前项目确实关心图像质量,因此我想从手机存储中获取原始位图(无压缩或分辨率损失)。
So, how can I get the high quality bitmap from intent
? 因此,如何从
intent
获得高质量的位图?
I use intent
to get a bitmap from the external storage in this way, 我使用
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();
}
}
And I get the bitmap from res
in this way, 我以这种方式从
res
获得位图,
imageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Intent has size limit to pass intent, So you can use public static object to do pass bitmap from service to broadcast Intent具有大小限制以传递Intent,因此您可以使用公共静态对象将位图从服务传递到广播
public class ImageBox {
public static Queue<Bitmap> mQ = new LinkedBlockingQueue<Bitmap>();
}
pass in service, 通过服务,
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();
}
});
}
BroadcastReceiver, 广播接收器,
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);
}
}
};
It should work for you. 它应该为您工作。 Let me know if any other help required.
让我知道是否需要其他帮助。
try this in you onActivityResult, 在您的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.