【发布时间】:2019-09-21 21:59:31
【问题描述】:
我想捕获图像并将其保存到内部存储中的特定文件夹。目前我能够打开意图并获取捕获图像的缩略图。我不想使用外部存储,因为现在大多数用户使用他们的内部存储而不是 sd 卡。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null){
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK){
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
LayoutInflater inflater = LayoutInflater.from(LeaveApplicationCreate.this);
final View view = inflater.inflate(R.layout.item_image,attachView, false);
ImageView img = view.findViewById(R.id.img);
AppCompatImageView btnRemove = view.findViewById(R.id.btnRemove);
img.setImageBitmap(imageBitmap);
btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attachView.removeView(view);
}
});
attachView.addView(view);
File directory = new File(Environment.getExternalStorageDirectory(),"/Digimkey/Camera/");
if (!directory.exists()) {
directory.mkdir();
}
File file = new File(directory, System.currentTimeMillis()+".jpg");
try (FileOutputStream out =new FileOutputStream(file)) {
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
既然你已经成功获取了图片,你可以直接使用OpenFileOutput(带有Serializable接口)将它保存到内部存储中。
-
“我不想使用外部存储,因为现在大多数用户使用的是内部存储而不是 sd 卡” -- external storage,从 Android SDK 的角度来看,不是 removable storage。用户无法直接访问 Android SDK 调用的 internal storage。
标签: android android-intent camera