将图片保存到Android

时间:2017-07-12 00:27:39

标签: java 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)

1 个答案:

答案 0 :(得分:2)

如果您希望它在您的图库中可见,则需要将其保存到更公开的位置。在将请求排入队列之前,请添加:

String fileName = "local.png";

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

这还需要您在manifest.xml中添加以下权限

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