2017-04-14 86 views
-2

我是Android新手。我想在URL的圖庫中保存image,並獲取保存圖像的文件名和路徑。所以我可以在畫廊中用onclick打開它。 我嘗試了很多解決方案,但找不到正確的方法來保存file path在gallary中保存圖像並獲取文件路徑android

我想要做的就是在圖庫中打開圖片URLonclick

我的代碼

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

您使用的需要FileProvider訪問圖像 –

+0

@DivyeshPatel我應該化爲烏有設備做在gallaery打開圖像。告訴我更多關於它的信息 – Vivek

+0

https:// inthecheesefactory。com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en –

回答

1

如果您targetSdkVersion是24或更高版本,我們必須使用FileProvider類來訪問特定的文件或文件夾,以使其可以訪問其他應用程序。

步驟來替換文件:// URI的內容:// 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()); 
+0

我不知道在哪裏使用createImageFile()方法 – Vivek

+0

此鏈接將幫助您https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en – Anmol

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); 
+0

你可以告訴我在哪裏使用createImageFile()方法。我沒有用它在我的代碼,也在你的評論。 – Vivek

+0

其中是createImageFile。我找不到 –

+0

你早先告訴你在圖像庫中打開'n'版本\t [inthecheesefactory.com/blog/...] – Vivek

相關問題