カメラで画像をキャプチャすると、このコードでエラーが発生します。私は、デバイスストレージ内のMyImagesフォルダにサムネイルを保存したいと思います。エラーが発生しましたカメラからキャプチャした画像のサムネイルを作成したい
:原因:java.lang.NullPointerException:file == null "at" fos = new FileOutputStream(thumbnailFile); forActivityResultのコードで
public class FragmentTwo extends Fragment {
Button btn1, btn2;
View v;
File image;
private static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_two, container, false);
btn1 = (Button) v.findViewById(R.id.btn1);
btn2 = (Button) v.findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "MyImages");
file.mkdirs();
File image = new File(file, "picture1" + ".jpg");
Uri uriSavedImage = Uri.fromFile(image);
i.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(i, REQUEST_IMAGE_CAPTURE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "picture2" + ".jpg");
Uri uriSavedImage = Uri.fromFile(image);
i.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivity(i);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final int THUMBSIZE = 64;
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile("/MyImages/picture1"),
THUMBSIZE, THUMBSIZE);
File thumbnailFile = image;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(thumbnailFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos);
try {
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
どのように修正する必要がありますか? @CommonsWare – Abhi