图像未保存在文件夹中

时间:2015-11-08 11:18:14

标签: android

我正在尝试创建一个文件夹并将图像保存在其中 但它不起作用。
我不知道我的代码有什么问题 - 你能告诉我为什么吗?

    // The method that invoke of uploading images
public   void openGallery() {


    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

        File folder = new File(this.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES), "albumName");


        File file = new File(this.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES), "fileName"+3);
        Bitmap imageToSave = (Bitmap) data.getExtras().get("data");
        try {
            FileOutputStream out = new FileOutputStream(file);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Uri selectedImage = data.getData();


        Intent i = new Intent(this,
                AddImage.class);
        i.putExtra("imagePath", selectedImage.toString());
        startActivity(i);


    }

修改

        final File path =
                Environment.getExternalStoragePublicDirectory
                        (
                              //  Environment.DIRECTORY_PICTURES + "/ss/"
                                //Environment.DIRECTORY_DCIM
                               Environment.DIRECTORY_DCIM + "/MyFolderName/"
                        );

        // Make sure the Pictures directory exists.
        if(!path.exists())
        {
            path.mkdirs();
        }
        Bitmap imageToSave = (Bitmap) data.getExtras().get("data");
        final File file = new File(path, "file" + ".jpg");
        try {
             FileOutputStream fos = new FileOutputStream(path);
            final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

            FileOutputStream out = new FileOutputStream(path);
            //fos = new FileOutputStream(path);
            imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fos);
           // imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:5)

我如何在DCIM中创建文件夹并在其中创建文件:

    /*
    Create a path where we will place our picture in the user's public
    pictures directory. Note that you should be careful about what you
    place here, since the user often manages these files.
    For pictures and other media owned by the application, consider
    Context.getExternalMediaDir().
    */
    final File path =
        Environment.getExternalStoragePublicDirectory
        (
            //Environment.DIRECTORY_PICTURES
            //Environment.DIRECTORY_DCIM
            Environment.DIRECTORY_DCIM + "/MyFolderName/"
        );

    // Make sure the Pictures directory exists.
    if(!path.exists())
    {
        path.mkdirs();
    }

    final File file = new File(path, fileJPG + ".jpg");

    try
    {
        final FileOutputStream fos = new FileOutputStream(file);
        final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

        //bmp.compress(CompressFormat.JPEG, 100, bos);
        bmp.compress(CompressFormat.JPEG, 85, bos);

        bos.flush();
        bos.close();
    }
    catch (final IOException e)
    {
        e.printStackTrace();
    }

fileJPG是我正在创建的文件名(动态添加日期) 将MyFolderName替换为albumName bmp是我的Bitmap数据(在我的例子中是截图)。

答案 1 :(得分:0)

我也花了很长时间来解决这个伪造的错误,最后只需在清单中添加这一行代码即可解决

    android:requestLegacyExternalStorage="true"
相关问题