Android 无法将图像从sd卡加载到gridview
嗨,我想把从相机拍摄的所有图像加载到网格视图中。但我有一个奇怪的例外。如果我只是尝试加载2或3个图像,所有的都进行得很顺利,但是当我尝试加载所有图像时,出现了错误。我的Android 无法将图像从sd卡加载到gridview,android,Android,嗨,我想把从相机拍摄的所有图像加载到网格视图中。但我有一个奇怪的例外。如果我只是尝试加载2或3个图像,所有的都进行得很顺利,但是当我尝试加载所有图像时,出现了错误。我的SD卡中大约有20个图像。这是我的代码 package com.example.imagegridview; import java.io.File; import java.util.ArrayList; import android.annotation.SuppressLint; import android.app.A
SD卡中大约有20个图像。这是我的代码
package com.example.imagegridview;
import java.io.File;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.widget.GridView;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
ArrayList<Bitmap> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String sdCardRootPath = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString()+"/Camera";
File rootFolder = new File(sdCardRootPath);
File[] picFiles = rootFolder.listFiles();
data = new ArrayList<Bitmap>();
for (File pic:picFiles) {
Bitmap b= BitmapFactory.decodeFile(pic.getAbsolutePath());
data.add(b);
}
GridView gv = (GridView) findViewById(R.id.gridview);
gv.setAdapter(new MAdapter(this,data));
}
}
你可能需要检查一下这个文件
我建议在直接添加图像之前,先缩放每个图像。
来自相机的图像实际上是一个相当大的尺寸,当你尝试添加20个这样的图像时,它肯定会在移动设备上溢出。
因此,在上传之前,请缩放每个图像,并确保其正常工作。在加载ie之前,请先调整位图大小,然后使用下面的代码对位图进行解码。`
public Bitmap decodeFile(File f, int size) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = size;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.outWidth = width_tmp;
o2.outHeight = height_tmp;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}`
公共位图解码文件(文件f,int size){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//找到正确的刻度值。它应该是2的幂。
所需最终整型尺寸=尺寸;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
o2.outWidth=宽度\u tmp;
o2.outHeight=高度\u tmp;
返回BitmapFactory.decodeStream(新文件输入流(f),null,o2);
}catch(filenotfounde异常){
}
返回null;
}`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
03-13 23:39:22.632: E/AndroidRuntime(13228): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:562)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:371)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:399)
03-13 23:39:22.632: E/AndroidRuntime(13228): at com.example.imagegridview.MainActivity.onCreate(MainActivity.java:32)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.app.ActivityThread.access$2300(ActivityThread.java:126)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.os.Looper.loop(Looper.java:123)
03-13 23:39:22.632: E/AndroidRuntime(13228): at android.app.ActivityThread.main(ActivityThread.java:4633)
03-13 23:39:22.632: E/AndroidRuntime(13228): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 23:39:22.632: E/AndroidRuntime(13228): at java.lang.reflect.Method.invoke(Method.java:521)
03-13 23:39:22.632: E/AndroidRuntime(13228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-13 23:39:22.632: E/AndroidRuntime(13228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-13 23:39:22.632: E/AndroidRuntime(13228): at dalvik.system.NativeStart.main(Native Method)
public Bitmap decodeFile(File f, int size) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = size;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.outWidth = width_tmp;
o2.outHeight = height_tmp;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}`