我是Android新手。我想在image
的库中保存URL
,并获取已保存图片的文件名和路径。所以我可以在库中用onclick
打开它。
我尝试了很多解决方案,但找不到正确的方法来保存file path
。
我要做的只是在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打开它。我也不知道它是否保存文件。
答案 0 :(得分:0)
使用它:
启动AsynchTask:
DownloadSelctedIMG d = new DownloadSelctedIMG();
d.execute();
现在AsynchTask类:
class DownloadSelctedIMG extends AsyncTask<String, String, Void> {
String ImgPath = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = new ProgressDialog(ReferActivity.this);
loading.setMessage("Please Wait..");
loading.setCancelable(false);
loading.show();
}
protected Void doInBackground(String... arg0) {
File f = new File("IMAGE_URL");
String filename = "image_name.jpg";
File wallpaperDirectory = new File(Environment.getExternalStorageDirectory().toString() + "/FOLDER/"
);
wallpaperDirectory.mkdirs();
ImgPath = wallpaperDirectory.getPath()+ filename;
String myu_recivedimage = "IMAGE_URL";
int count;
try {
URL myurl = new URL(myu_recivedimage);
URLConnection conection = myurl.openConnection();
int lenghtOfFile = conection.getContentLength();
conection.connect();
int filelength = conection.getContentLength();
InputStream inpit = new BufferedInputStream(myurl.openStream());
OutputStream output = new FileOutputStream(ImgPath);
byte data[] = new byte[1024];
long total = 0;
while ((count = inpit.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
inpit.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (loading != null && loading.isShowing()) {
loading.dismiss();
}
try {
/*--- SAVED IMAGE PATH--ImgPath ---------*/
File file = new File(ImgPath);
} catch (Exception e) {
}
}
}
对于图库中的打开图像:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri photoURI = FileProvider.getUriForFile(getApplicationContext(),
BuildConfig.APPLICATION_ID + ".provider",
new File(ImgPath));
intent.setDataAndType(photoURI, "image/*");
startActivity(intent);
答案 1 :(得分:0)
如果您的targetSdkVersion为24或更高,我们必须使用FileProvider类来访问特定文件或文件夹,以使其可供其他应用访问。
使用content:// uri:
替换file:// uri的步骤将此添加到您的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<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>
</application>
</manifest>
然后在res文件夹下的xml文件夹中创建一个provider_paths.xml文件。如果文件夹不存在,则可能需要创建文件夹。该文件的内容如下所示。它描述了我们希望在名称为external_files的根文件夹(path =“。”)中共享对外部存储的访问。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
最后一步是在
中更改下面的代码行Uri photoURI = Uri.fromFile(createImageFile());
到
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());