【发布时间】:2016-03-10 22:02:18
【问题描述】:
我无法从已写入位图的文件中检索位图。 显示没有错误。但图像显示不正确。 我将位图转换为字节,然后存储在内部缓存中
从相机获取图像
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();