问题描述
我想从我的Android应用程序上传到服务器几张照片。我得到了内存不足的错误。我的code图像选择如下
I am trying to upload few photos from my android application to the server. I am getting the out of memory error. My code for image selection is as follows
code触发
public void triggerImageSelection(View view) {
int triggeringView = Integer.parseInt((String) view.getTag());
Log.v(DBUG, "tag of the view:" + triggeringView);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
triggeringView);
}
在的onActivityResult code的一部分如下:
onActivityResult part of the code is as follows:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Log.v(DBUG, "request code:: " + requestCode);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
filePaths[0] = getPath(data.getData());
File filewe = new File(getPath(data.getData()));
Bitmap bitmap1 = BitmapFactory.decodeFile(getPath(data
.getData()));
mAdImage1.setImageBitmap(bitmap1);
if (filewe != null)
Log.v(DBUG, "got file" + filePaths[0]);
mAdImage1.setVisibility(View.VISIBLE);
}
break;
在的getPath();函数返回的文件路径和是如下:
the getPath(); function returns the file path and is as follows
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,
null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String filePath = cursor.getString(column_index);
cursor.close();
return filePath;
}
我有选择四象四个按钮。它工作正常的第一次,但第二次它显示内存不足的异常。我怎样才能解决这个?
i have four buttons for selecting four images. It works fine for the first time but in second time it shows out of memory exception. How can i tackle with this?
推荐答案
虽然从文件读取尽量减少图象的质量如下
While reading from the file try to reduce the quality of the image as follows
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
Bitmap bitmap1 = BitmapFactory.decodeFile(getPath(data.getData()),options);
mAdImage1.setImageBitmap(bitmap1);
bitmap1.recycle();
bitmap1=null;
如果问题仍然存在,使用的android:largeHeap =真正的
清单文件的应用程序标签
If the problem still persists, use android:largeHeap="true"
in the application tag of manifest file.
这篇关于内存不足多个照片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!