【发布时间】:2015-05-24 12:01:21
【问题描述】:
在 bello 示例中,我将数组作为附加对象,我创建了一个数组 imagePath[]
它存储来自gridview的所有选定图像的路径,但我的问题是当我这样做时
它用于gridview中的第一张图像然后它工作正常但是当我选择另一个图像或
public class ShowImages extends Activity {
String title1[],title[],imagePath[];
Bitmap[] mResources;
int cnt=0,j=0;
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutxml);
iv=(ImageView)findViewById(R.id.imageView1);
Bundle extras = getIntent().getExtras();
title1 = getIntent().getExtras().getStringArray("imageArray");
for(int i=0;i<title1.length;i++){
if(title1[i] != null){
cnt++;
}
}
imagePath=new String[cnt];
for(int i=0;i<title1.length;i++){
if(title1[i] != null){
if(j<cnt){
imagePath[j]=title1[i];
j++;
}
}
}
// Toast.makeText(getApplicationContext(),String.valueOf(cnt), Toast.LENGTH_LONG).show();
mResources=new Bitmap[cnt];
try{
for(int i=0;i<cnt;i++)
{
Toast.makeText(getApplicationContext(),imagePath[i], Toast.LENGTH_LONG).show();
File imgFile = new File(imagePath[i]);
if(imgFile.exists())
{
mResources[i] = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
// iv.setImageBitmap(mResources[i]);
}
}
}
catch(Exception e){
}
}
}
gridview 中的多个图像它在 logcat 中给出以下错误
05-24 17:10:12.616: E/art(3371): Throwing OutOfMemoryError "Failed to allocate a 63701004 byte allocation with 524288 free bytes and 46MB until OOM"
05-24 17:10:12.635: E/AndroidRuntime(3371): FATAL EXCEPTION: main
05-24 17:10:12.635: E/AndroidRuntime(3371): Process: com.customgallery, PID: 3371
05-24 17:10:12.635: E/AndroidRuntime(3371): java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte allocation with 524288 free bytes and 46MB until OOM
【问题讨论】:
-
第一个也是重要的问题,你为什么要创建位图数组?有关我的问题的更多信息,请查看以下问题:Strange out of memory issue while loading an image to a Bitmap object
-
@PareshMayani 我必须这样做,因为在 php 页面中我必须发送 base64 字符串 ..所以我需要将位图数组转换为 base64 字符串数组
-
但是创建位图数组会导致同样的问题!而是将选择的图像 URI/URL 存储到字符串数组中,并在上传到服务器时将其转换为位图或 Base64!
-
@PareshMayani 如果可以在服务器上转换图像 int Base64 的路径(我的意思是 php 页面)你能给我提供代码
-
这是一个菜鸟问题,您的服务器如何能够通过图像 URI 从您的设备获取实际图像?
标签: android arrays android-intent android-imageview android-bitmap