我一直在尝试不同的东西,我可以将照片保存在用相机拍摄的SD卡中(非意图)但不能从SD卡中取出相同的照片并将其放在ImageView中。我总是得到一个Null指针Exception。
不知道遗失了什么,希望有人可以帮助我:
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// Save the image JPEG data to the SD card
FileOutputStream fos = null;
String fileName = "";
try {
fileName = "/mnt/sdcard/DCIM/MyPicture.jpg";
fos = new FileOutputStream(fileName);
fos.write(data);
fos.close();
Toast.makeText(getBaseContext(), "Image saved:" ,
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Log.e(TAG, "File Note Found", e);
Toast.makeText(getBaseContext(), "Image couldn't be saved.",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e(TAG, "IO Exception", e);
Toast.makeText(getBaseContext(), "Image couldn't be saved.",
Toast.LENGTH_LONG).show();
}
Bitmap bitmap = BitmapFactory.decodeFile(fileName);
Log.d(TAG, fileName);
mImageView.setImageBitmap(bitmap);
}
};
我也试过了:
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
+ "/DCIM/MyPhoto.jpg");
mImageView.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
}
它会保存图片,但在尝试将图像放入ImageView时说“读取文件时出错”
logcat的:
DDMS:
对不起大家的头疼......我忘了把mImageView =(Imageview)findViewById(R.id.imageView1);
我是一场灾难: - )
答案 0 :(得分:0)
试试这个,
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
mImageView.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
还要检查您的imageview mImageView
是否已初始化
答案 1 :(得分:0)
使用它。它会对你有帮助。
public class LoadImagesFromSDCard extends AsyncTask<String, Void, Void> {
private ProgressDialog Dialog = new ProgressDialog(CameraPhotoCapture.this);
Bitmap mBitmap;
protected void onPreExecute() {
/****** NOTE: You can call UI Element here. *****/
//UI Element
Dialog.setMessage("Loading image from Sdcard..");
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null;
try {
/** Uri.withAppendedPath Method Description
* Parameters
* baseUri Uri to append path segment to
* pathSegment encoded path segment to append
* Returns
* a new Uri based on baseUri with the given segment appended to the path
*/
uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]);
/************** Decode an input stream into a bitmap. *********/
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
if (bitmap != null) {
/********* Creates a new bitmap, scaled from an existing bitmap. ***********/
newBitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true);
bitmap.recycle();
if (newBitmap != null) {
mBitmap = newBitmap;
}
}
} catch (IOException e) {
//Error fetching image, try to recover
/********* Cancel execution of this task. **********/
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.
// Close progress dialog
Dialog.dismiss();
if(mBitmap != null)
showImg.setImageBitmap(mBitmap);
}
}