我是Android新手。我想在URL
的圖庫中保存image
,並獲取保存圖像的文件名和路徑。所以我可以在畫廊中用onclick
打開它。 我嘗試了很多解決方案,但找不到正確的方法來保存file path
。在gallary中保存圖像並獲取文件路徑android
我想要做的就是在圖庫中打開圖片URL
在onclick
。
我的代碼
activity_edit_profile_basic_imageview_profile_icon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
galleryAddPic();
}
});
和
private void galleryAddPic() {
String fileName = intentProfilePic.substring(intentProfilePic.lastIndexOf('/') + 1);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(intentProfilePic);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
showImage(fileName);
}
和
public void showImage(String fileName) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" +"/sdcard/"+fileName), "image/*");
startActivity(intent);
}
連我自己都不知道我在正確的方式或錯誤行爲。
當我點擊我的ImageView的應用程序崩潰和錯誤說...
android.os.FileUriExposedException: file:///sdcard/4f5eac461a2a75431d392dd71ffebf97.jpg exposed beyond app through Intent.getData()
我知道我是走錯了路。所以我想知道如何獲取文件路徑並保存圖像。並再次使用filepath在onclink中打開它。我也不知道它是否保存文件。
您使用的需要FileProvider訪問圖像 –
@DivyeshPatel我應該化爲烏有設備做在gallaery打開圖像。告訴我更多關於它的信息 – Vivek
https:// inthecheesefactory。com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en –