我在{strong> provider_paths.xml 文件中使用FileProvider.getUriForFile
。我做错了,我没有。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="StrengGeheim" path="."/>
</paths>
这是我在 标签内的AndroidManifest.xml中添加的:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
这是我的相机意图代码:
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA);
}
private Uri getOutputMediaFileUri() {
try {
File file = getOutputMediaFile();
return FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file);
}
catch (IOException ex){
ex.printStackTrace();
Toast.makeText(getContext(), MESSAGE_FAILED, Toast.LENGTH_SHORT).show();
}
return null;
}
private File getOutputMediaFile() throws IOException {
File encodeImageDirectory =
new File(Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
if (!encodeImageDirectory.exists()) {
encodeImageDirectory.mkdirs();
}
String uniqueId = UUID.randomUUID().toString();
File mediaFile = new File(encodeImageDirectory, uniqueId + ".png");
mediaFile.createNewFile();
return mediaFile;
}
在文件Uri中,路径类似于 /StrengGeheim/StrengGeheim/21a70d51-3375-4d44-b698-0727b6f90065.png ,同时创建媒体文件以存储捕获的图像但存储图像的路径来自gallery / storage / emulated / 0 / StrengGeheim / 06673da7-876f-4a53-9a3d-288ae033f7ac 。我在拍摄照片和应用程序正在关闭时遇到错误。
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /StrengGeheim/StrengGeheim/21a70d51-3375-4d44-b698-0727b6f90065.png (No such file or directory)
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
W/System.err: at com.stegano.strenggeheim.fragment.FragmentEncode.saveImage(FragmentEncode.java:166)
W/System.err: at com.stegano.strenggeheim.fragment.FragmentEncode.onActivityResult(FragmentEncode.java:143)
W/System.err: at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151)
W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:7235)
W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4320)
W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)
W/System.err: at android.app.ActivityThread.-wrap19(Unknown Source:0)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:105)
W/System.err: at android.os.Looper.loop(Looper.java:164)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这就是我处理OnActivityResult的方式:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_CANCELED) {
return;
}
try {
if (requestCode == GALLERY && data != null) {
Bitmap bitmap = getBitmapFromData(data, getContext());
File mediaFile = getOutputMediaFile();
String path = saveImage(bitmap, mediaFile);
Log.println(Log.INFO, "Message", path);
Toast.makeText(getContext(), MESSAGE_IMAGE_SAVED, Toast.LENGTH_SHORT).show();
loadImage.setImageBitmap(bitmap);
imageTextMessage.setVisibility(View.INVISIBLE);
} else if (requestCode == CAMERA) {
File file = new File(fileUri.getPath());
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
loadImage.setImageBitmap(bitmap);
saveImage(bitmap, file);
Toast.makeText(getContext(), MESSAGE_IMAGE_SAVED, Toast.LENGTH_SHORT).show();
imageTextMessage.setVisibility(View.INVISIBLE);
}
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(getContext(), MESSAGE_FAILED, Toast.LENGTH_SHORT).show();
}
}
private Bitmap getBitmapFromData(Intent intent, Context context){
Uri selectedImage = intent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return BitmapFactory.decodeFile(picturePath);
}
private String saveImage(Bitmap bmpImage, File mediaFile) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmpImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
try {
FileOutputStream fo = new FileOutputStream(mediaFile);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(getContext(),
new String[]{mediaFile.getPath()},
new String[]{"image/png"}, null);
fo.close();
return mediaFile.getAbsolutePath();
} catch (IOException ex) {
ex.printStackTrace();
}
return "";
}
有人可以帮忙吗?
答案 0 :(得分:1)
File file = new File(fileUri.getPath());
作为I pointed out previously,这是错误的。摆脱这条线。
您需要的File
是您在创建Uri
EXTRA_OUTPUT
值时使用的fileUri
。
saveImage()
。
此外:
请加载位图并在后台线程上执行磁盘I / O
请不要浪费用户创建第二个图片文件的时间,因为您已有一个(IOW,当您已经拥有时,请不要致电MapControl.Children.Add()
文件)