我有问题。我使用bumtech glid库,当我点击并尝试捕捉相机图像时,它总是会出错:
6-01 02:17:06.217 1696-1696/com.sportfriend.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sportfriend.app, PID: 1696
android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/Sportfriend/SF20170601021706.jpg exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:845)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8941)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8926)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at com.sportfriend.app.GalleryActivityMain.cameraIntent(GalleryActivityMain.java:140)
at com.sportfriend.app.GalleryActivityMain.getPhoto(GalleryActivityMain.java:82)
at com.sportfriend.app.GalleryActivityMain.access$000(GalleryActivityMain.java:28)
at com.sportfriend.app.GalleryActivityMain$1.onClick(GalleryActivityMain.java:56)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
块引用
public class GalleryActivityMain extends AppCompatActivity {
private Button btnPhoto;
private GridView gridView;
private GridViewAdapter gridAdapter;
private List<String> imageItems;
private String imagePath="";
private File myDir;
private File filePath;
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int REQUEST_EXTERNAL_STORAGE_RESULT = 2;
private static final int REQUEST_CAMERA_RESULT = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_gallery);
createAppDirectory();
imagePath = Environment.getExternalStorageDirectory().toString()+"/Sportfriend";
imageItems = new ArrayList<>();
btnPhoto = (Button) findViewById(R.id.btnPhoto);
btnPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getPhoto();
}
});
getImages();
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(this, R.layout.grid_item, imageItems);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String imagePath = ((TextView) v.findViewById(R.id.text)).getText().toString();
Intent intent = new Intent(GalleryActivityMain.this, DetailActivity.class);
intent.putExtra("imagePath", imagePath);
startActivity(intent);
}
});
gridView.setAdapter(gridAdapter);
}
private void getPhoto(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if ((ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)){
if ((ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){
cameraIntent();
}else {
if (shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(this, "Berechtigung zum Speichern ben�tigt", Toast.LENGTH_LONG).show();
}
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_EXTERNAL_STORAGE_RESULT);
}
}else {
if (shouldShowRequestPermissionRationale(android.Manifest.permission.CAMERA)){
Toast.makeText(this,"Berechtigung zum Speichern ben�tigt",Toast.LENGTH_LONG).show();
}
requestPermissions(new String[]{android.Manifest.permission.CAMERA},REQUEST_CAMERA_RESULT);
}
}else {
cameraIntent();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int [] grantResults){
if (requestCode == REQUEST_EXTERNAL_STORAGE_RESULT){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
getPhoto();
}else {
Toast.makeText(this,"Berechtigung verweigert !!!",Toast.LENGTH_LONG).show();
}
}else if (requestCode == REQUEST_CAMERA_RESULT){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
getPhoto();
}else {
Toast.makeText(this, " Kein Zugriff auf Camera",Toast.LENGTH_LONG).show();
}
}else {
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
}
private void createAppDirectory(){
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
myDir = new File(root + "/Sportfriend");
if (!myDir.exists()){
myDir.mkdirs();
}
}
private void cameraIntent(){
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault());
Date date = new Date();
String sDate = formatter.format(date);
String imageName = "SF" + sDate + ".jpg";
try{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
filePath = new File(myDir, imageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(filePath));
startActivityForResult(intent , REQUEST_IMAGE_CAPTURE);
}catch (ActivityNotFoundException e){
String errorText = "Funktion wird auf Ihren Ger�t nicht Unterst�tzt!";
Toast.makeText(this, errorText,Toast.LENGTH_LONG).show();
}
}
private List<String> getImages(){
new File(imagePath ).mkdirs();
File fileTarget = new File(imagePath);
File[] files = fileTarget.listFiles();
imageItems.clear();
if (files!=null){
for (File file: files){
imageItems.add(file.getAbsolutePath());
}
}
return imageItems;
}
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data)
{
getImages();
gridAdapter.notifyDataSetChanged();
}
}
&#13;
尝试使用Fileprovider,但它没有运行。
答案 0 :(得分:1)
private void cameraIntent()
{
if (Build.VERSION.SDK_INT >= 23)
{
File path = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
if (!path.exists()) path.mkdirs();
File image = new File(path, "image.jpg");
Uri imageUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", image);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CAMERA);
}
else
{
File path = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
if (!path.exists()) path.mkdirs();
File image = new File(path, "image.jpg");
Uri imageUri = Uri.fromFile(image);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
}