在Android中将位图图像存储到SD卡

发布于 2024-12-11 01:08:57 字数 776 浏览 4 评论 0原文

我的 Android 代码面临一些奇怪的问题, 我在位图变量中有一个图像,想要将该文件保存到 SD 卡。 我的代码如下,

Bitmap IMAGE // Loaded from internet servers.;
try {
    File _sdCard = Environment.getExternalStorageDirectory();
    File _picDir  = new File(_sdCard, "MyDirectory");
    _picDir.mkdirs();

    File _picFile = new File(_picDir,  "MyImage.jpg");
    FileOutputStream _fos = new FileOutputStream(_picFile);
    IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, _fos);
    _fos.flush();
    _fos.close();
    Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception ex) {
    ex.printStackTrace();
    Toast.makeText(this, ex.getMessage(), 7000).show();
}

我使用索尼 Experia Arc 作为我的测试设备,当手机连接到我的计算机时,代码运行良好,它存储图像并显示在图库中。但是,当我断开手机与计算机的连接并测试该应用程序时,它不会保存图片,也不会显示任何异常。

I am facing some strange problem with my android code,
I have an image in Bitmap variable and want to save that file to SD card.
I code as follow,

Bitmap IMAGE // Loaded from internet servers.;
try {
    File _sdCard = Environment.getExternalStorageDirectory();
    File _picDir  = new File(_sdCard, "MyDirectory");
    _picDir.mkdirs();

    File _picFile = new File(_picDir,  "MyImage.jpg");
    FileOutputStream _fos = new FileOutputStream(_picFile);
    IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, _fos);
    _fos.flush();
    _fos.close();
    Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception ex) {
    ex.printStackTrace();
    Toast.makeText(this, ex.getMessage(), 7000).show();
}

I am using Sony Experia Arc as my testing device, when the phone is connected to my computer, the code works nice, it stores image and also displays in gallery. But when I disconnect phone from my computer and test the app, it doesn't save picture and doesn't show any exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天荒地未老 2024-12-18 01:08:57

使用此功能

 void saveImage() {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");

    String fname = "Image.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

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

检查此答案将提供更多详细信息Android将文件保存到外部存储

use this function

 void saveImage() {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");

    String fname = "Image.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

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

Check this answer will give more details Android saving file to external storage

云淡风轻 2024-12-18 01:08:57
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        //4
        File file = new File(Environment.getExternalStorageDirectory()+File.separator +        "image.jpg");
        try {
            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);
            //5
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        //4
        File file = new File(Environment.getExternalStorageDirectory()+File.separator +        "image.jpg");
        try {
            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);
            //5
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
锦上情书 2024-12-18 01:08:57
**This Code Cover the Following Topics**

1. Save a bitmap Image on sdcard a jpeg
2. Create a folder on sdcard
3. Create every file Separate name
4. Every file save with date and time
5. Resize the image in very small size
6. Best thing image Quality fine not effected from Resizing


The following method is used to create an image file using the bitmap

公共无效createImageFromBitmap(位图bmp){

    FileOutputStream fileOutputStream = null;
    try {

        // create a File object for the parent directory
        File wallpaperDirectory = new File("/sdcard/Capture/");
        // have the object build the directory structure, if needed.
        wallpaperDirectory.mkdirs();

        //Capture is folder name and file name with date and time
        fileOutputStream = new FileOutputStream(String.format(
                "/sdcard/Capture/%d.jpg",
                System.currentTimeMillis()));

        // Here we Resize the Image ...
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100,
                byteArrayOutputStream); // bm is the bitmap object
        byte[] bsResized = byteArrayOutputStream.toByteArray();


        fileOutputStream.write(bsResized);
        fileOutputStream.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
}


   and add this in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
**This Code Cover the Following Topics**

1. Save a bitmap Image on sdcard a jpeg
2. Create a folder on sdcard
3. Create every file Separate name
4. Every file save with date and time
5. Resize the image in very small size
6. Best thing image Quality fine not effected from Resizing


The following method is used to create an image file using the bitmap

public void createImageFromBitmap(Bitmap bmp) {

    FileOutputStream fileOutputStream = null;
    try {

        // create a File object for the parent directory
        File wallpaperDirectory = new File("/sdcard/Capture/");
        // have the object build the directory structure, if needed.
        wallpaperDirectory.mkdirs();

        //Capture is folder name and file name with date and time
        fileOutputStream = new FileOutputStream(String.format(
                "/sdcard/Capture/%d.jpg",
                System.currentTimeMillis()));

        // Here we Resize the Image ...
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100,
                byteArrayOutputStream); // bm is the bitmap object
        byte[] bsResized = byteArrayOutputStream.toByteArray();


        fileOutputStream.write(bsResized);
        fileOutputStream.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
}


   and add this in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文