Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 访问SD卡映像_Android_Bitmap_Sd Card - Fatal编程技术网

Android 访问SD卡映像

Android 访问SD卡映像,android,bitmap,sd-card,Android,Bitmap,Sd Card,我正在开发一个android应用程序,其中我必须将图像从SD卡上传到互联网。为此,我使用MediaStore内容提供程序并从MediaStore.Images.Media.DATA列获取图像路径,现在我要做的是使用位图.decodeFile(字符串路径)从该路径创建位图方法,然后在图像视图列表中显示该位图。我面临的问题是,当我尝试转到在列表中显示此图像的活动时,我遇到了如下错误10-20 11:10:47.070:error/AndroidRuntime(922):java.lang.OutOf

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


有谁能给我建议解决这个问题的方法,或是实现我目标的替代方法吗。提前感谢。

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

使用下面的行获取位图后

Bitmap bmp = Bitmap.decodeFile(String path) 
您应该尝试将其缩小到所需的大小

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

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

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

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);

在将图像设置为ImageView之前,最好使用图像代码的调整大小。您的意思是我无法获取您的信息。请对此评论多加说明。请调整所选图像的大小。当你调整图像大小时,它会减小大小以避免内存不足,例外你的位图使用了太多的内存,所以在谷歌上使用OOM调整它的大小搜索你会收到成千上万的帖子图像文件的大小是多少?
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()
{
}