将字符串转换为URI为位图在ImageView的显示 [英] Converting string to uri to bitmap to display in an ImageView

查看:167
本文介绍了将字符串转换为URI为位图在ImageView的显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找遍了一个解决我的问题,只是似乎无法弄清楚。我敢肯定,这大概是1或2个简单的线条,希望有人能指导我在正确的方向。

I have looked all over for a solution to my problem and just can't seem to figure it out. I'm sure it is probably 1 or 2 simple lines and hopefully someone can steer me in the right direction.

在我的应用程序,用户可以点击一个按钮,将打开画廊。一旦他们选择一个图像,它会显示的图像在我的应用程序的ImageView的。这部分工作完全正常。本来,我只是从画廊返回一个URI,我会直接显示这一点:

In my app, a user can click a button that will open the gallery. Once they select an image, it will display that image in an ImageView within my app. That part is working perfectly fine. Originally, I just had it return a uri from the gallery and I would directly display that with this:

imageView1.setImageURI(myUri);

嗯,很明显我现在遇到了可怕的内存不足的错误,如果用户重新加载该页面几次连续,所以我不得不清理我的code,以缩小图像。我已经通过实施将图像成位图和规模下来了我一个位图类来完成这一点。现在,我的ImageView的显示code是这样的:

Well, obviously I am now running into the dreaded "Out Of Memory" error if the user reloads that page several times in a row so I'm having to clean up my code to scale down the image. I have done this by implementing a bitmap class that turns the image into a bitmap and scales it down for me. Now, my ImageView display code looks like this:

imageView1.setImageBitmap(bitmap1);

这部分工作正常,以及。这里的问题是:

That part is working fine as well. HERE IS THE PROBLEM:

我的URI路径转换为字符串,然后保存在共享preference。这是这样,当用户退出应用程序和回来以后,它们设置图像自动显示。我转换的URI是这样的:

I convert the uri path to a string and then save that in a SharedPreference. This is so that when the user exits the application and the comes back later, the image that they set automatically displays. I convert the uri like this:

...
selectedImageUri = data.getData();
String selectedImagePath;
selectedImagePath = getPath(selectedImageUri);
...

旧的方法来检索共享preference字符串,将其转换为URI,然后显示它是工作的罚款。 (除外出当然的内存错误),它是这样的:

The old method to retrieve the SharedPreference String, convert it to uri, then display it was working fine. (except for the Out Of Memory error of course) It looked like this:

Uri myUri = Uri.parse(selectedImagePath);
imageView1 = setImageURI(myUri);

selectedImagePath显然,我的共享preference检索到的字符串。同样,这工作得很好,但如果重装太多次会抛出的错误。

"selectedImagePath" is obviously the String that I retrieved from the SharedPreference. Again, this worked fine but would throw the error if reloaded too many times.

这是不是现在的工作的一部分是,当我尝试实施新的位图转换,这样我可以扩展位图,并没有得到内存错误。这里是code为:

The part that IS NOT WORKING now is when I try to implement the new Bitmap conversion so that I can scale the bitmap and not get the memory error. Here is the code for that:

Uri myUri = Uri.parse(selectedImagePath)
Bitmap bitmap = getThumbnail(myUri);
imageView1.setImageBitmap(bitmap);

这显示任何内容。原始图像选择显示图像细腻,但是当我回到这个画面,它试图解析从共享preference字符串,然后将其转换为位图,从来都没有显示。在code为getThumbnail的方法是直接从这个职位采取了--->

This displays nothing. The original image choosing displays the image fine but when I return to this screen and it tries to parse the string from the SharedPreference and then convert it to the bitmap, nothing ever displays. The code for the "getThumbnail" method was taken directly from THIS POST --->

得到位图的乌里[机器人]

这是第三个答案了。

任何人有什么想法?很抱歉的超长职位,但我宁愿在解释我的问题比没有提供足够的信息。很抱歉,如果这是回答了其他地方。我一直在通过猎其他问题几个小时,刚刚没有发现任何解决我的问题。

Anyone have any ideas? Sorry for the super long post but I'd rather over explain my problem than not give enough info. Sorry if this was answered somewhere else. I've been hunting through other questions for hours and have just not found anything that solves my problem.

感谢。

推荐答案

我想通了所以这里是我做的任何人有这个特殊的问题。后的图像选择从画廊,它返回的意图,我通过这个code得到了这一意图的数据:

I figured it out so here is what I did for anyone else having this unique problem. After the image is chosen from the gallery and it returns the intent, I got the data from that intent via this code:

selectedImageUri = data.getData();

然后,我得到了这条道路,通过这样的:

Then I got the path from that via this:

selectedImagePath = getPath(selectedImageUri);

这打了一个电话给这个getPath的方法:

Which made a call to this "getPath" method:

public String getPath(Uri uri)  
{ 
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
} 

然后我救了selectedImagePath作为共享preference字符串。

Then I saved "selectedImagePath" as a SharedPreference string.

后来,检索该字符串,并将其转换回显示图像时,我第一次检索的共享preference串并转换回selectedImagePath。然后,我在这样的ImageView的设置:

Later, to retrieve that string and convert it back to showing an image, I first retrieved the SharedPreference string and converted it back to "selectedImagePath". Then, I set it in the ImageView like this:

targetImage = (ImageView)findViewById(R.id.imageView1);
targgetImage.setImageBitmap(decodeSampledBitmapFromResource(selectedImagePath, 200, 200));

这使一个呼叫到以下方法:

which made a call to the following methods:

public 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 = 2;

if (height > reqHeight || width > reqWidth) {
    if (width > height) {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    } else {
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }
}
return inSampleSize;

}

public static Bitmap decodeSampledBitmapFromResource(String resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(resId, options);
}

这是一个很大的code做了一个相当简单的任务,赫克,但它的工作原理,所以我很高兴,并继续前进。希望这会帮助别人谁需要完成同样的事情。

It's a heck of a lot of code to do a fairly simple task but it works so I'm happy and moving on. Hopefully this will help someone else who needs to accomplish the same thing.

这篇关于将字符串转换为URI为位图在ImageView的显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆