0

我在网上找到了这段代码来将位图保存到 Android 的内部存储中,但它似乎没有保存,或者我只是找不到图像。如果要这样做,这会将图像保存在哪里?

public boolean saveImageToInternalStorage(Bitmap image) {

    try {
        // Use the compress method on the Bitmap object to write image to
        // the OutputStream
        FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);

        // Writing the bitmap to the output stream
        image.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();

        return true;
    } catch (Exception e) {
        Log.e("saveToInternalStorage()", e.getMessage());
        return false;
    }
}

谢谢

4

1 回答 1

1

请参阅此博客文章,其中简要说明:

http://rajareddypolam.wordpress.com/?p=3&preview=true

&

试试这个方法:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_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);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

我想你忘记的是写图片的权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
于 2013-08-15T09:51:07.247 回答