2017-07-12 177 views
-2

我正在嘗試製作網絡瀏覽器,因此我想保存圖像並使用以下代碼。將圖像保存到Android上的下載文件夾中

private EditText editText; 
private WebView webView; 

private Button button3; 

DownloadManager downloadManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    editText = (EditText) findViewById(R.id.editText); 
    button3 = (Button) findViewById(R.id.button3); 


    webView = (WebView) findViewById(R.id.webView); 

    webView.setWebViewClient(new WebViewClient()); 

    webView.getSettings().setJavaScriptEnabled(true); 



    button3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (button3.isClickable()) { 

       String a = webView.getUrl(); 


       downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE); 
       Uri uri = Uri.parse(a); 
       DownloadManager.Request request = new DownloadManager.Request(uri); 
       request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       Long reference = downloadManager.enqueue(request); 



      }} 


       }); 

好,我可以下載圖片時,我按下按鈕3,是的,我可以將其保存的圖像,但該航線/data/data/com.android.providers.downloads/cache/ ..

所以我看不到畫廊上的圖像,我希望有人可以幫助我將圖像保存到另一個文件夾,例如默認情況下下載。

我得到了logcat的

07-12 01:14:57.543 1305-1305/? E/EGL_emulation: tid 1305: eglCreateSyncKHR(1641): error 0x3004 (EGL_BAD_ATTRIBUTE) 

07-12 01:14:52.081 7102-7102/com.example.th.pp W/art: Attempt to remove non-JNI local reference, dumping thread 
07-12 01:14:52.097 7102-7102/com.example.th.pp W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 7102 
07-12 01:14:52.431 7102-7102/com.example.th.pp W/art: Attempt to remove non-JNI local reference, dumping thread 
07-12 01:14:56.982 2167-2179/android.process.media W/DownloadManager: Path appears to be invalid: /storage/emulated/0/Pictures/file.png 
07-12 01:14:56.985 2167-2179/android.process.media E/DatabaseUtils: Writing exception to parcel 
    java.lang.SecurityException: No permission to write to /storage/emulated/0/Pictures/file.png: Neither user 10076 nor current process has android.permission.WRITE_EXTERNAL_STORAGE. 
     at android.app.ContextImpl.enforce(ContextImpl.java:1613) 
     at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1645) 
     at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:707) 
     at com.android.providers.downloads.DownloadProvider.checkFileUriDestination(DownloadProvider.java:756) 
     at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:573) 
     at android.content.ContentProvider$Transport.insert(ContentProvider.java:264) 
     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163) 
     at android.os.Binder.execTransact(Binder.java:565) 
07-12 01:14:56.988 7102-7102/com.example.th.pp D/AndroidRuntime: Shutting down VM 
07-12 01:14:56.988 7102-7102/com.example.th.pp E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.th.pp, PID: 7102 
    java.lang.SecurityException: No permission to write to /storage/emulated/0/Pictures/file.png: Neither user 10076 nor current process has android.permission.WRITE_EXTERNAL_STORAGE. 
     at android.os.Parcel.readException(Parcel.java:1683) 
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) 
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 
     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) 
     at android.content.ContentResolver.insert(ContentResolver.java:1274) 
     at android.app.DownloadManager.enqueue(DownloadManager.java:1014) 
     at com.example.think.pp.MainActivity$2.onClick(MainActivity.java:102) 
     at android.view.View.performClick(View.java:5610) 
     at android.view.View$PerformClick.run(View.java:22265) 
     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:6077) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

回答

2

如果你想在你的畫廊看到這個錯誤,你需要把它保存到更多的公共場所。在你排隊的請求,添加:

String fileName = "local.png"; 

request.setDestinationInExternalPublicDir(// 
    Environment.DIRECTORY_PICTURES, fileName); 

這也將要求您添加以下權限在你的manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
+0

您好,感謝在我入隊之前寫你的代碼,我嘗試下載和形象,但應用程序停下來,我是新的Android和這裏也是,即時通訊學習英語,所以我的英語,它不是很好,只要我寫了答案,我看到一個人標記我的英語錯誤,並退出我有一些點xd –

+0

當應用程序停止時,它會在[LogCat](https://developer.android.com/studio/debug/am-logcat.html)中產生一條錯誤消息。在你的問題中發佈堆棧跟蹤。 –

+0

謝謝,我添加到我的問題的紅色的錯誤,我會希望你的回答 –

相關問題