将位图保存在Android中,作为JPEG在文件夹中的外部存储中



我正在使用此代码将位图保存在外部存储中,但是如果不存在该文件夹,则不会创建文件夹:

String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOutputStream = null;
        File file = new File(path + "/Captures/", "screen.jpg");
        try {
            fOutputStream = new FileOutputStream(file);
            capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
            fOutputStream.flush();
            fOutputStream.close();
            MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        }

如果不存在,我该如何将图像保存在新目录中并在设备中的文件夹中保存默认值?

尝试一下,它将确定结果:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
    file.delete();
try {
    FileOutputStream out = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

添加此内容以在画廊中显示:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

查看此链接以获取清晰的答案: 在画廊中显示文件夹图像

请使用以下代码段可能会有所帮助

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
if (!file.exists()) {
    file.mkdirs();
}
try {
    fOutputStream = new FileOutputStream(file);
    capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
    fOutputStream.flush();
    fOutputStream.close();
    MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
    e.printStackTrace();
    Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
    return;
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
    return;
}

使用以下内容:

File dir = new File(path + "/Captures/");
if(!dir.exists()) {
    dir.mkdirs();
}
File file = new File(path + "/Captures/", "screen.jpg");
 ......

迟到,但可能对某人有帮助。使用以下代码,由于BufferOutputStream,它将在外部目录中保存位图。

public boolean storeImage(Bitmap imageData, String filename) {
    // get path to external storage (SD card)
    File sdIconStorageDir = null;
    sdIconStorageDir = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/myAppDir/");
    // create storage directories, if they don't exist
    if (!sdIconStorageDir.exist()) {
        sdIconStorageDir.mkdirs();
    }
    try {
        String filePath = sdIconStorageDir.toString() + File.separator + filename;
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        //Toast.makeText(m_cont, "Image Saved at----" + filePath, Toast.LENGTH_LONG).show();
        // choose another format if PNG doesn't suit you
        imageData.compress(Bitmap.CompressFormat.PNG, 100, bos);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }
    return true;
}

您应该在文件文档中查看,您会找到方法mkdir()。它与Unix One几乎相同:https://developer.android.com/reference/java/io/file.html#mkdir()

这就是我在科尔汀的情况下的做法:

fun getImageUriFromBitmap(context: Context, bitmap: Bitmap?): Uri {
        var filePath = ""
        try {
            filePath = createAndGetFilePath(context)
            val fileOutputStream = FileOutputStream(filePath)
            val bos = BufferedOutputStream(fileOutputStream)
            bitmap?.compress(Bitmap.CompressFormat.PNG, 100, bos)
            bos.flush()
            bos.close()
        } catch (e: FileNotFoundException) {
            Log.w("TAG", "Error saving image file: " + e.message)
        } catch (e: IOException) {
            Log.w("TAG", "Error saving image file: " + e.message)
        }
        return Uri.parse(filePath)
    }
    private fun createAndGetFilePath(context: Context): String {
        val file = File(
            context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), // you can change the path here
            BuildConfig.localImagesFolder // add it in build.gradle under android
        )
        try {
            if (!file.exists()) {
                file.mkdirs()
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        return file.absolutePath + "/" + System.currentTimeMillis() + ".jpg"
    }

最新更新