在图库android中保存图像

and*_*oid 5 android gallery

我正在使用此代码保存图像:

URL url = null;
            try {
                url = new URL("image");
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }
            Bitmap bmp = null;
            try {
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            OutputStream output;
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File(filepath.getAbsolutePath() + "/folder name/");
            dir.mkdirs();
            File file = new File(dir, image + ".png");
            Toast.makeText(HomeActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
            try {

                output = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
                output.flush();
                output.close();

            }

            catch (Exception e) {
                e.printStackTrace();
            }
Run Code Online (Sandbox Code Playgroud)

问题是当此代码在棒棒糖设备上运行时,图像未显示在图库中.我必须安装文件管理器来检查这些图像.

使用此代码:

    MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "image";
Run Code Online (Sandbox Code Playgroud)

图像保存在相机文件夹中.

我想在所有Android设备中显示具有特定文件夹名称的图库中的图像.

请帮忙.

and*_*oid 13

public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
    //Create Path to save Image
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
    path.mkdirs();
    File imageFile = new File(path, imgName+".png"); // Imagename.png
    FileOutputStream out = new FileOutputStream(imageFile);
    try{
        bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
        out.flush();
        out.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                 Log.i("ExternalStorage", "Scanned " + path + ":");
                 Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch(Exception e) {
        throw new IOException();
    }
}
Run Code Online (Sandbox Code Playgroud)

}

为我工作.谢谢你的时间


Exi*_*e05 0

只需更改这些行,它就会起作用 -

File filepath = Environment.getExternalStorageDirectory().toString();
        File dir = new File(filepath + "/folder name/");
        dir.mkdirs();
        File file = new File(dir, image + ".png");
Run Code Online (Sandbox Code Playgroud)