android.os.FileUriExposedException:file.jpg通过ClipData.Item.getUri()暴露在app之外

android.os.FileUriExposedException:file.jpg通过ClipData.Item.getUri()暴露在app之外,第1张

概述我尝试按下一个打开相机并拍照的按钮.我的代码在这里//forimportscheckonbottomofthiscodeblockpublicclassHomeProfileActivityextendsAppCompatActivity{//ButtoncamerapublicstaticfinalStringTAG=HomeProfileActivity.class.getSimpleName();public

我尝试按下一个打开相机并拍照的按钮.我的代码在这里

//for imports check on bottom of this code blockpublic class HomeProfileActivity extends AppCompatActivity {//button camerapublic static final String TAG = HomeProfileActivity.class.getSimplename();public static final int REQUEST_TAKE_PHOTO = 0;public static final int REQUEST_TAKE_VIDEO = 1;public static final int REQUEST_PICK_PHOTO = 2;public static final int REQUEST_PICK_VIDEO = 3;public static final int MEDIA_TYPE_IMAGE = 4;public static final int MEDIA_TYPE_VIDEO = 5;private Uri mMediaUri;private ImageVIEw photobutton;private button buttonUploadImage, buttonTakeImage;protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_home_profile);    ButterKnife.bind(this);}@OnClick(R.ID.buttonTakeImage)voID takePhoto() {    mMediaUri = getoutputMediafileUri(MEDIA_TYPE_IMAGE);    if (mMediaUri == null) {        Toast.makeText(this,                "There was a problem accessing your device's external storage.",                Toast.LENGTH_LONG).show();    }    else {        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);        startActivityForResult(takePhotoIntent, REQUEST_TAKE_PHOTO);    }}@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == RESulT_OK){        if (requestCode == REQUEST_TAKE_PHOTO) {            Intent intent = new Intent(this, VIEwImageActivity.class);            intent.setData(mMediaUri);            startActivity(intent);        }    }    else if (resultCode != RESulT_CANCELED){        Toast.makeText(this, "Sorry, there was an error", Toast.LENGTH_SHORT).show();    }}private Uri getoutputMediafileUri(int mediaType) {    // check for external storage    if (isExternalStorageAvailable()) {        // get the URI        // 1. Get the external storage directory        file mediaStorageDir = getExternalfilesDir(Environment.DIRECTORY_PICTURES);        // 2. Create a unique file name        String filename = "";        String fileType = "";        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());        if (mediaType == MEDIA_TYPE_IMAGE) {            filename = "img_" + timeStamp;            fileType = ".jpg";        } else if (mediaType == MEDIA_TYPE_VIDEO) {            filename = "VID_" + timeStamp;            fileType = ".mp4";        } else {            return null;        }        // 3. Create the file        file mediafile;        try {            mediafile = file.createTempfile(filename, fileType, mediaStorageDir);            Log.i(TAG, "file: " + Uri.fromfile(mediafile));            // 4. Return the file's URI            return Uri.fromfile(mediafile);        }        catch (IOException e) {                Log.e(TAG, "Error creating file: " +                        mediaStorageDir.getabsolutePath() + filename + fileType);            }        }        // something went wrong        return null;    }private boolean isExternalStorageAvailable(){    String state = Environment.getExternalStorageState();    if (Environment.MEDIA_MOUNTED.equals(state)){        return true;    }    else {        return false;    }}import androID.content.Intent;import androID.content.SharedPreferences;import androID.net.Uri;import androID.os.Bundle;import androID.os.Environment;import androID.provIDer.MediaStore;import androID.support.annotation.NonNull;import androID.support.design.Widget.BottomNavigationVIEw;import androID.support.v7.app.AppCompatActivity;import androID.util.Log;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import java.io.file;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import butterknife.ButterKnife;import butterknife.OnClick;

我在方法onclick中的startActivityForResult也有问题
和import java.text.SimpleDateFormat;也跳转到异常运行时
我正在使用buildtoolsversion sdk 25

解决方法:

除了使用fileProvIDer的解决方案之外,还有另一种方法可以解决这个问题.简单的说

 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build());

在Application.onCreate()方法中.通过这种方式,VM忽略文件URI暴露.

总结

以上是内存溢出为你收集整理的android.os.FileUriExposedException:file.jpg通过ClipData.Item.getUri()暴露在app之外全部内容,希望文章能够帮你解决android.os.FileUriExposedException:file.jpg通过ClipData.Item.getUri()暴露在app之外所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100701.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存