访问SD卡镜像

发布于 2024-12-11 02:48:44 字数 479 浏览 1 评论 0原文

我正在开发一个 Android 应用程序,其中我必须将图像从 SD 卡上传到互联网。为此,我使用 MediaStore 内容提供程序并从 MediaStore.Images.Media.DATA 列获取图像路径,现在我正在做的是创建一个 位图 从该路径使用 Bitmap.decodeFile(String path) 方法,然后在 ListImageView 中显示该位图。我面临的问题是,当我尝试转到在列表中显示此图像的活动时,我收到如下错误 10-20 11:10:47.070: ERROR/AndroidRuntime(922): java.lang .OutOfMemoryError:位图大小超出VM预算

任何人都可以建议我解决此问题或实现我的目标的替代方法。提前致谢。

I am developing an android application in which I have to upload image from sdcard to internet. For this I am using MediaStore content provider and getting the image path from MediaStore.Images.Media.DATA column and now what I am doing is that creating a bitmap from that path using Bitmap.decodeFile(String path) method and then displaying that bitmap in a ImageView in a List. The problem I am facing is when I try to go to the activity which is showing this image in list I am getting an error like this 10-20 11:10:47.070: ERROR/AndroidRuntime(922): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

Can anybody suggest me solution to this problem or an alternative way to achieve my goal. Thanks in advance.

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

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

发布评论

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

评论(3

一直在等你来 2024-12-18 02:48:44

这种情况发生很多次,因为存储在 SD 卡中的图像具有高分辨率,并且您的应用程序无法将图像大小处理为堆大小,

在使用以下行获取位图后,

Bitmap bmp = Bitmap.decodeFile(String path) 

您应该尝试缩放它减小到您需要的尺寸

newBmp = Bitmap.createScaledBitmap(bmp, 240, 320, true);

,然后使用 newBmp 在您的视图中进行设置。

This happens many time cause the image stored in the sdcard are of high resolution and the your app can't handle the size of image into heap size

After Using the below line to get Bitmap

Bitmap bmp = Bitmap.decodeFile(String path) 

You should try scaling it down to your required size

newBmp = Bitmap.createScaledBitmap(bmp, 240, 320, true);

and then use the newBmp to set in your view.

因为看清所以看轻 2024-12-18 02:48:44

下面的代码用于调整所选图像的大小

Bitmap bitmap  = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth = 100;
            int newHeight = 100;

            // calculate the scale - in this case = 0.4f
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;

            // createa matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);

            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                    width, height, matrix, true); 

            ((ImageView) findViewById(R.id.ImageviewTest)).setImageBitmap(resizedBitmap);

Below code is used to resize the selected image

Bitmap bitmap  = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int newWidth = 100;
            int newHeight = 100;

            // calculate the scale - in this case = 0.4f
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;

            // createa matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);

            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                    width, height, matrix, true); 

            ((ImageView) findViewById(R.id.ImageviewTest)).setImageBitmap(resizedBitmap);
平生欢 2024-12-18 02:48:44
String filepath = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    File file = new File(filepath, filename);
    FileInputStream fs;
    try {
        fs = new FileInputStream(file);
        BitmapFactory.Options bfOptions = new BitmapFactory.Options();
        /*
         * bfOptions.inDither=false; //Disable Dithering mode
         * bfOptions.inPurgeable=true; //Tell to gc that whether it needs
         * free memory, the Bitmap can be cleared
         * bfOptions.inInputShareable=true;
         */
        bfOptions.inJustDecodeBounds = false;
        bfOptions.inTempStorage = new byte[32 * 1024];
        Bitmap b = BitmapFactory.decodeFileDescriptor(fs.getFD(), null,
                bfOptions);
        img.setImageBitmap(b);
}
catch()
{
}
String filepath = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    File file = new File(filepath, filename);
    FileInputStream fs;
    try {
        fs = new FileInputStream(file);
        BitmapFactory.Options bfOptions = new BitmapFactory.Options();
        /*
         * bfOptions.inDither=false; //Disable Dithering mode
         * bfOptions.inPurgeable=true; //Tell to gc that whether it needs
         * free memory, the Bitmap can be cleared
         * bfOptions.inInputShareable=true;
         */
        bfOptions.inJustDecodeBounds = false;
        bfOptions.inTempStorage = new byte[32 * 1024];
        Bitmap b = BitmapFactory.decodeFileDescriptor(fs.getFD(), null,
                bfOptions);
        img.setImageBitmap(b);
}
catch()
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文