[英]How to capture an image and store it with the native Android Camera
我在從本地相機應用程序捕獲圖像並將其存儲時遇到問題。 這是我的一些代碼示例。
_path = Environment.getExternalStorageDirectory() + "make_machine_example.jpg";
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, 0 );
拍照后,我又回到了原來的活動,當我通過Android DDMS File Explorer導航到我的SD卡時,照片就不在那里了。 有人知道為什么不保存嗎?
如果有人仍在訪問此線程,這是最終產品:
public class CameraCapture extends Activity {
protected boolean _taken = true;
File sdImageMainDirectory;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
File root = new File(Environment
.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
sdImageMainDirectory = new File(root, "myPicName");
startCameraActivity();
}
} catch (Exception e) {
finish();
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
}
protected void startCameraActivity() {
Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 0:
finish();
break;
case -1:
try {
StoreImage(this, Uri.parse(data.toURI()),
sdImageMainDirectory);
} catch (Exception e) {
e.printStackTrace();
}
finish();
startActivity(new Intent(CameraCapture.this, Home.class));
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {
_taken = true;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);
}
public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
Bitmap bm = null;
try {
bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
FileOutputStream out = new FileOutputStream(imageDir);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
bm.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
您是否檢查過Environment.getExternalStorageDirectory()的輸出是什么,因為如果它不包含尾隨文件分隔符(/),那么您的映像將最終位於一個不位於SD卡上的目錄中,例如:
/mnt/sdcardmake_machine_example.jpg
當您真正想要的是:
/mnt/sdcard/make_machine_example.jpg
請嘗試以下代碼:
_path = Environment.getExternalStorageDirectory() + File.separator + "make_machine_example.jpg";
1。 只需使用
new File(Environment.getExternalStorageDirectory(), "make_machine_example.jpg");
而且不要理會分隔符。
2。 以前也遇到過同樣的問題。 SenseUI手機具有不創建文件的自定義相機應用程序。 您正在使用什么設備? 它可能已在最新設備中修復,但仍然可能是一個問題。 因此,這里有一個完整的示例,介紹了如何克服它。 將照片保存到文件時出現問題 。
保存圖像后應執行媒體掃描
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
將此行添加到AndroidManifest.xml文件中,並刪除擴展名make_machine_example:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
它將開始捕獲照片並將其存儲到SD卡中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.