0

我只想知道如何将我从图库中选择的图像保存到我自己的文件夹中以及重命名它,但所有属性都应该保持不变。好吧,我已经有了这个方法,它来自 vogella 的教程,您可以在其中从图库中选择并将其显示为预览。但我只想做我上面提到的事情,而不是预览。这是代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    /*this method is used for getting and selecting image from gallery and display it in the imageView*/
    InputStream stream = null;
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            if (bitmap != null) {
                bitmap.recycle();
            }
            stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);

            preview.setImageBitmap(bitmap);
            //instead setting it in preview, save it in your image folder and rename it
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
}

好吧,我确实知道一些关于保存文件的部分,但我真的很迷茫,所以希望有人能帮助我。提前致谢。

4

2 回答 2

1
private String saveToInternalSorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir("directoryName", Context.MODE_PRIVATE);
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {
           // fos = openFileOutput(filename, Context.MODE_PRIVATE);

            fos = new FileOutputStream(mypath);

            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return directory.getAbsolutePath();
    }
于 2013-06-25T08:12:08.553 回答
1

做这个

public String writeFileToInternalStorage(Context context, Bitmap outputImage) {
   String fileName = Long.toString(System.currentTimeMillis()) + ".png";

   final FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
   outputImage.compress(CompressFormat.PNG, 90, fos);
}
于 2013-06-25T08:12:58.707 回答