如何将相机拍摄的照片通过我的应用程序保存到设备的图库中?
问题是应用程序在拍照后当前崩溃,并且只能在指定的目录中保存单个图像。 我是正确的假设应用程序崩溃,因为它不知道保存文件后该怎么办?我如何解决这个问题,返回语句之后的任何内容都是“无法访问的代码”。
代码:
public class Activity_Camera extends Activity implements View.OnClickListener {
public static final int cameraData = 1;
ImageButton ib;
ImageView iv;
Intent i;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initialise();
}
private void initialise() {
iv = (ImageView) findViewById(R.id.ivPicReturn);
ib = (ImageButton) findViewById(R.id.ibTakePic);
ib.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
//
startActivityForResult(i, cameraData);
break;
}
}
//
private Uri getOutputMediaFileUri() {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "DCIM/TradeIt");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
return uriSavedImage;
}
//
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
logcat的
W/dalvikvm(919): threadid=1: thread exiting with uncaught exception (group=0x41d7d930)
03-07 00:00:24.704: E/AndroidRuntime(919): FATAL EXCEPTION: main
03-07 00:00:24.704: E/AndroidRuntime(919): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.hyperlocal.tradeit/com.hyperlocal.tradeit.Activity_Camera}: java.lang.NullPointerException
03-07 00:00:24.704: E/AndroidRuntime(919): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
答案 0 :(得分:1)
你必须增加一个变量并保存并将其添加到你的文件名中,以便在你获得Uri时制作更多图片,并且你的图片中显示的图片会这样做:
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
如果您在拍摄照片后没有像这样发送广播,则直到下次手机开启时才会更新图库