我有多个活动应该在一个图像上工作,这个图像通过Intent获取,如此处所述 https://developer.android.com/training/camera/photobasics.html 然后,我有一个我的图像的路径,我用它来访问照片并操纵它(改变大小,颜色) 但是当我尝试从路径创建一个位图时,它总是会给出像
这样的错误E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
我将imagePath保存在私有变量中,并将其传递给活动之间的Bundle,这在第一次正常工作。当我以相同的方式将其传递给第三个活动时,abobe错误显示。
这里有一些我的代码在将图像放入ImageView之前缩放图像:
public class scaleImage {
static Bitmap setPic(ImageView mImageView, String mCurrentPhotoPath) {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
//bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap);
return bitmap;
}
}
第一个Activity中的函数,用于创建保存图像的文件(如示例中所示):
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
imageFileName = image.getName();
return image;
}
第二个Activity在调用时通过Bundle获取imagePath,并以相同的方式将其传递给第三个:
void callPictureScalerActivity() {
Intent intent = new Intent(this, ConvertToGrayscale.class);
Bundle b = new Bundle();
b.putString("imagePath", mCurrentPhotoPath);
intent.putExtras(b);
startActivity(intent);
}
第三个Activity尝试从imagepath获取一个不再有效的Bitmap。 (不必在onCreate中执行此操作,因为还不知道imageView的大小):
public void onWindowFocusChanged(boolean focus) {
super.onWindowFocusChanged(focus);
scaleImage.setPic((ImageView) findViewById(R.id.scalablePicture), photoPath);
}
所以我想知道,在调用多个活动后,图像文件是否被删除,或者是其他地方的错误?在这一点上,我无法以其他方式访问图像。
修改:上面的错误出现在我尝试从路径中获取位图的行中:
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
所以它是在decodeFile函数中发生的错误,我无法查看。
当在第二个按下按钮时调用第三个Activity,调用callPictureScalerActivity函数。
照片以下列功能保存:
void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.w("Error", "Error creating the image file");
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
我知道照片已保存,因为我从mCurrentPhotoPath查找路径。