gpt4 book ai didi

java - 拍摄照片到外部存储<智能手机中的图库>

转载 作者:行者123 更新时间:2023-12-02 03:43:03 37 4
gpt4 key购买 nike

我无法使用相机拍照,并且它没有保存在外部目录中并显示在 imageView 中。

在activity_main:有用于拍照的按钮,用于显示图像的imageView。

在 AndroidManifest.xml 中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

class MainActivity{    
private Uri mImgFileUri;
void onCreate(){ ...

Button button = (Button)findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageCapture();
}
});

}

private void imageCapture() {
File extDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

File myPicDir = new File(extDir.getAbsolutePath()+"/MyPictures");
if(!myPicDir.exists()){
myPicDir.mkdirs();
}
Date dt = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyMMdd-HHmmss");

String fileName = dateFormat.format(dt)+".jpg";
File imgFile = new File(myPicDir+"/"+fileName);
mImgFileUri = Uri.fromFile(imgFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,mImgFileUri);
startActivityForResult(intent,555);
}

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == 555 && resultCode == RESULT_OK){
String path = mImgFileUri.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(path);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
}
}
}

出现此错误

Error :

Process: com.example.cameraimagecapture, PID: 22554
android.os.FileUriExposedException: file:///storage/emulated/0/DCIM/MyPictures/20190630-165910.jpg exposed beyond app through ClipData.Item.getUri()
...MainActivity.imageCapture(MainActivity.java:57) >> startActivityForResult(intent,555);
...MainActivity.access$000(MainActivity.java:23) >> class MainActivity extends AppCompatActivity {}
...MainActivity$1.onClick(MainActivity.java:36) >> imageCapture();

最佳答案

用于将图像显示到屏幕上。

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);


if (resultCode != RESULT_CANCELED) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}


}
}

使用屏幕截图将照片保存到外部存储:

1) 在 Activity 中配置您的布局。示例:RelativeLayout

RelativeLayout RelativeLayout;

RelativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);

2)像下面的代码一样检测位图格式的 View 。

 private Bitmap getscreenshot(View view) {
View v = view;
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
return bitmap;
}

3)设置此代码来保存您的图像。

private void SaveImage(Bitmap finalBitmap) {

String root = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();

int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
// sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
// Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
out.flush();
out.close();

} catch (Exception e) {
e.printStackTrace();
}

MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}

4)最后,使用所有这些代码来达到您的 Intent :

 Bitmap screenshota = getscreenshot(RelativeLayout);
SaveImage(screenshota);

关于java - 拍摄照片到外部存储<智能手机中的图库>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823820/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com