如何在基于应用程序的文件夹中保存网址图像?

时间:2018-05-22 05:29:16

标签: android bitmap directory

我从数据库下载图像并保存下载,但希望将其保存在我自己的应用程序命名文件夹中。

using (var conn = OpenConnection())
{
    using (var cmd = new NpgsqlCommand("CREATE TABLE foo (id INT PRIMARY KEY)", conn))
        cmd.ExecuteNonQuery();
    using (var cmd = new NpgsqlCommand("INSERT INTO foo (id) VALUES (1)", conn))
        cmd.ExecuteNonQuery();
    using (var cmd = new NpgsqlCommand("INSERT INTO foo (id) VALUES (1)", conn))
        cmd.ExecuteNonQuery();
}

保存我使用的图像助手位图,所以我想知道如何添加文件夹来保存这些图像

SaveImageHelper

package com.blipclap.creativegraphy.Helper;

fabDownload = (FloatingActionButton) findViewById(R.id.fabDownload);
        fabDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //check permission
                if (ActivityCompat.checkSelfPermission(ViewWallpaper.this,
                        android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, Common.PERMISSION_REQUEST_CODE);
                } else {
                    AlertDialog dialog = new SpotsDialog(ViewWallpaper.this);
                    dialog.show();
                    dialog.setMessage("Please wait......");

                    String fileName = UUID.randomUUID().toString() + ".png";
                    Picasso.with(getBaseContext())
                            .load(Common.select_background.getImageLink())
                            .into(new SaveImageHelper(getBaseContext(),
                                    dialog,
                                    getApplicationContext().getContentResolver(),
                                    fileName,
                                    "CreativeGraphy Live Wallpaper Image"));
                }

            }
        });

文件夹应该用app name命名,在这里如何添加文件夹以保存图像文件夹

2 个答案:

答案 0 :(得分:0)

File folder = new File(Environment.getExternalStorageDirectory() +File.separator + "YOUR_APP_NAME");
   boolean success = true;
   if (!folder.exists()) {
      success = folder.mkdirs();
   }
   if (success) {
       try {
           bm.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(Environment.getExternalStorageDirectory() +
                File.separator + "YOUR_APP_NAME" + File.separator + "file_name" + ".jpg")));
                Toast.makeText(getActivity(), "Downloaded", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
                } else {
                            // Do something else on failure
                        }

请尝试以上代码

答案 1 :(得分:0)

在SaveImageHelper类中下载后调用它:

private void saveImage(Bitmap bitmap,String fileName) { 
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        File downloadfolder= new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/NewFolder");
        if (!downloadfolder.exists()) {
            downloadfolder.mkdir();
        }
        File file = new File(downloadfolder, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(byteArray, 0, byteArray.length);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
}