[英]Cannot get Bitmap from File
我无法从已将位图写入其中的文件中检索位图。 没有显示错误,但是图像显示不正确。 我将位图转换为字节,然后存储在内部缓存中
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"pic.jpg");
img = Uri.fromFile(photo);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, img);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
Bitmap takenImage =BitmapFactory.decodeFile(img.getPath());
File folder = getCacheDir();
File myFile = new File(folder, "myImage.bmp");
FileOutputStream fos = new FileOutputStream(myFile);
FileInputStream fis = new FileInputStream(img.getPath());
byte[] image = new byte[fis.available()];
fos.write(image);
fos.flush();
fos.close();
fis.close();
File folder = getCacheDir();
File myFile = new File(folder, "myImage.bmp");
Bitmap bmp=BitmapFactory.decodeFile(myFile.getAbsolutePath());
imgv.setImageBitmap(bmp);
此代码正确吗?
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
尝试像这样转换位图
您正在创建图像文件两次,并且据我的看法,您的代码将如下所示 :-
File myFile = new File( Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"pic.jpg");
Bitmap bmp=BitmapFactory.decodeFile(myFile.getAbsolutePath());
imgv.setImageBitmap(bmp);
-请原谅我的解释不佳,但如果仍然无法解决,您可以分享或再次寻求帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.