gpt4 book ai didi

java - 保存图像后检查 SD 卡 (Android)

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

我的 Intent 是完成后将图像保存到/sdcard/Aperture。虽然图像确实保存,但保存后不会立即显示图像。可能是什么问题呢?我尝试在“Activity 结果”中使用扫描广播,但这会导致崩溃,并且不会扫描图像。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SETTINGS_REQUEST && resultCode == Activity.RESULT_OK) {
MediaFragment content = (MediaFragment) getFragmentManager().findFragmentById(com.marlonjones.aperture.R.id.content_frame);
if (content != null) content.reload();
reloadNavDrawerAlbums();
}
if (requestCode == NEW_PICTURE) {
// return from file upload
if (resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (data != null) {
uri = data.getData();
}
if (uri == null && mCameraFileName != null) {
uri = Uri.fromFile(new File(mCameraFileName));
}
}
}
}

final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
public void camera(MenuItem menu) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
Date date = new Date();
DateFormat df = new SimpleDateFormat("-mm-ss");

String newPicFile = "PH" + df.format(date)+".jpg";
String outPath = "/sdcard/Aperture/" + newPicFile;
File outFile = new File(outPath);
mCameraFileName = outFile.toString();
Uri outuri = Uri.fromFile(outFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
startActivityForResult(intent, NEW_PICTURE);
}
else{
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
Date date = new Date();
DateFormat df = new SimpleDateFormat("-mm-ss");

String newPicFile = "PH" + df.format(date) + ".jpg";
String outPath = "/sdcard/Aperture/" + newPicFile;
File outFile = new File(outPath);
mCameraFileName = outFile.toString();
Uri outuri = Uri.fromFile(outFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
startActivityForResult(intent, NEW_PICTURE);}
}

最佳答案

我认为 Impression 在某些地方使用了 MediaScannerConnection。这是使用扫描仪 Intent 的更现代版本。它还会告诉您扫描何时完成,并为您提供新的 URI(如果文件已从媒体数据库中删除,则为 null)。

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

扫描完成后您需要重新加载媒体和相册。

关于java - 保存图像后检查 SD 卡 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447116/

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