我想调用系统相机并在系统视图中拍摄大量照片,我还想将这些照片存储在某些文件夹中。我搜索了一些代码,如下所示:
Intent imageCaptureIntent = new Intent("android.media.action.STILL_IMAGE_CAMERA");
File out = new File(Environment.getExternalStorageDirectory(), "camera"+System.currentTimeMillis()+".jpg");
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent, 1);
此代码可以让我在系统视图中拍摄大量照片,但它无法存储我想要的文件夹(Environment.getExternalStorageDirectory()+“camera”+ System.currentTimeMillis()+“。jpg”)。那么,谁有一个很好的方法来确保我的照片存储在我想要的文件夹中。谢谢你的进步!
答案 0 :(得分:3)
在你的onCreate()中执行此操作,
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PICTURE_WITH_CAMERA);
在onActivityResult中,
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode != RESULT_OK)
return;
try {
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File tmpFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
FileOutputStream fos = new FileOutputStream(tmpFile);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (IOException io_e) {
// TODO: handle error
}
}
答案 1 :(得分:1)
在上面的代码中没有创建它存储在默认文件夹中的文件夹
但此代码是特定文件夹中的商店图片
public class MainActivity extends ActionBarActivity {
Button btnImage;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.imageView);
btnImage= (Button) findViewById(R.id.buttonCapture);
btnImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode != RESULT_OK)
return;
try {
AssetFileDescriptor imageAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r");
FileInputStream fis = imageAsset.createInputStream();
File root=new File(Environment.getExternalStorageDirectory(),"/Imagesss/");//Folder Name Imagesss
if (!root.exists()) {
System.out.println("No directory");
root.mkdirs();
}
File file;
file=new File(root,"android_"+System.currentTimeMillis()+".jpg" );
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (IOException io_e) {
// TODO: handle error
}
//Display Data In Image View
if (requestCode == 0 && resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}