如何传递没有得到OutOfMemory错误两项活动之间的图像路径? [英] How do you pass an image path between two activities without getting outofMemory error?
问题描述
因此,基本上,用户点击一个按钮,它从库中抓取的图像。然后该图像被发送到要被显示另一个活动。这是我抢的形象我的第一个活动。
私人无效grabImage()
{
意图imageGetter =新意图(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(imageGetter,RESULT_LOAD_IMAGE);
}@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
super.onActivityResult(要求code,结果code,数据);
如果(要求code == RESULT_LOAD_IMAGE&放大器;&安培;结果code == RESULT_OK&放大器;&安培;!NULL =数据)
{
乌里selectedImage = data.getData();
的String [] = filePathColumn {MediaStore.Images.Media.DATA}; // 1数组大小,我们把在一个字符串
光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst();
INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
user_image_path = cursor.getString(参数:columnIndex); //这里,我们有我们的形象路径。
cursor.close();
ImageView的ImageView的=(ImageView的)findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.de codeFILE(user_image_path));
} 意图theIntent =新意图(这一点,CardModel.class);
theIntent.putExtra(的ImagePath,user_image_path);
}
现在这是一个尝试显示图像我的第二个活动。
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.card_model_layout);
串grabImagePath = getIntent()getStringExtra(的ImagePath);
ImageView的IMG =(ImageView的)findViewById(R.id.userImage);
img.setImageBitmap(BitmapFactory.de codeFILE(grabImagePath)); }
我不断收到的OutOfMemoryError错误,我不想加:
机器人:largeHeap =真来解决问题。
有人可以给我一个干净的切割为例(与所有code)对如何正确传递图像两项活动之间(使用它的字符串路径)。我敢肯定,很多开发者可以从中受益。谢谢!
另外有没有办法做到这一点,而无需调用的onActivityResult,反而只是让你自己的方法,并把code在那里,并呼吁,在onCreate()方法。
事实是 -
既然你用有限的内存的工作,理想情况下,你只需要
在存储器加载较低分辨率版本。较低的分辨率
版本应该匹配其显示UI组件的大小。一个
具有较高分辨率的图像不提供任何可见的好处,
但是仍然占据precious内存并造成额外的性能
由于开销额外动态缩放。
块引用>[参考链接: http://developer.android.com /training/displaying-bitmaps/load-bitmap.html]
这就是为什么你需要缩小图像。希望下面的code可以帮助你!
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;公共类BitmapUtility { 公共静态位图德codeSampledBitmapFromResource(字符串路径,INT reqWidth,诠释reqHeight){ //首先去code。与inJustDe codeBounds = true来检查尺寸
最后BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(路径选择); //计算inSampleSize
options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight); //德code位与inSampleSize集
options.inJustDe codeBounds = FALSE;
返回BitmapFactory.de codeFILE(路径选择);
}
私有静态诠释calculateInSampleSize(
BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
//原始高度和图像宽度
最终诠释身高= options.outHeight;
最终诠释宽度= options.outWidth;
INT inSampleSize = 1; 如果(高度> reqHeight ||宽度GT; reqWidth){ 最终诠释halfHeight =身高/ 2;
最终诠释半宽度=宽度/ 2; //计算最大inSampleSize值是2的幂,并保持这两个
//高度和宽度大于所请求的高度和宽度。
而((halfHeight / inSampleSize)GT; reqHeight
&功放;&安培; (半角/ inSampleSize)GT; reqWidth){
inSampleSize * = 2;
}
} 返回inSampleSize;
}
}您可以使用上面code如下:
而不是使用的:imageView.setImageBitmap(BitmapFactory.de codeFILE(user_image_path));
您可以使用:
<$p$p><$c$c>imageView.setImageBitmap(BitmapUtilty.de$c$cSampledBitmapFromResource(\"path/to/image\",300,400)
So basically, a user click a button and it grabs an image from the gallery. Then that image is sent to another activity to be displayed. This is my first activity where I grab the image.
private void grabImage() { Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(imageGetter, RESULT_LOAD_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); user_image_path = cursor.getString(columnIndex);//here we have our image path. cursor.close(); ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setImageBitmap(BitmapFactory.decodeFile(user_image_path)); } Intent theIntent = new Intent(this,CardModel.class); theIntent.putExtra("imagePath", user_image_path); }
Now this is my second activity that tries to display that image.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.card_model_layout); String grabImagePath = getIntent().getStringExtra("imagePath"); ImageView img = (ImageView) findViewById(R.id.userImage); img.setImageBitmap(BitmapFactory.decodeFile(grabImagePath)); }
I keep getting OutOfMemoryError error and I don't wanna add: android:largeHeap="true" to solve the problem.
Can someone give me a clean cut example (with all the code) of how to properly pass an image (using its string path) between two activities. I'm sure a lot of developers could benefit from this. Thanks!
Also is there a way to do this without calling onActivityResult and instead just make you're own method and put the code in there and call that in the onCreate() method.
解决方案The fact is-
Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory. The lower resolution version should match the size of the UI component that displays it. An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.
[Reference Link : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html]
That's why you need to scale down images . Hope, following code will help you !
import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class BitmapUtility { public static Bitmap decodeSampledBitmapFromResource(String path,int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path,options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); } private static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }
You can use above code as follows: Instead of using:
imageView.setImageBitmap(BitmapFactory.decodeFile(user_image_path));
you can use:
imageView.setImageBitmap(BitmapUtilty.decodeSampledBitmapFromResource("path/to/image",300,400)
这篇关于如何传递没有得到OutOfMemory错误两项活动之间的图像路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!