图片从画廊到imageview [英] Picture from gallery to an imageview

查看:106
本文介绍了图片从画廊到imageview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从图库中选择一个图像到ImageView,但是它不起作用.

i'm trying to select an image from gallery to an ImageView but it doesen't work.

代码如下:

public class Profilo extends MainActivity {

private ImageView img;
private Bitmap bmp;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profilo);

    img = (ImageView)findViewById(R.id.profile_image);

    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 1);
        }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                String filePath = null;
                if (requestCode == 1) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    filePath = cursor.getString(columnIndex);
                    cursor.close();

                    if (bmp != null && !bmp.isRecycled()) {
                    }
                    bmp = null;
                }
                bmp = BitmapFactory.decodeFile(filePath);
                img.setBackgroundResource(0);
                img.setImageBitmap(bmp);
            }
            else
            {
                Log.d("Status:", "Photopicker canceled");
            }
         }
    });
}

这是布局:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/profile_image"
    android:contentDescription="@string/profile_image"
    android:src="@drawable/profilo_facebook"
    android:layout_gravity="center_horizontal"/>

</LinearLayout>

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sidacri.testapp" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
<activity android:name=".Profilo"
        android:label="@string/label_profilo">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>
</manifest>

让我知道问题出在哪里,请:)我只是尝试了一些方法,但是它永远都行不通

Let me know where's the problem, please :) i've just tried some methods but it never doesen't work

推荐答案

找出代码中的错误:

String filePath = null;

因此您将空值传递给您的 DecodeFile 方法,因此您的位图为空.如果要将图像设置为ImageView,则可以执行以下操作,而无需像这样将其转换为Bitmap:

so you are passing the null value to your DecodeFile method and so your Bitmap is null. If you want to set the image to ImageView,you can do that without converting it to Bitmap like this:

YourImageView.setImageURI(selectedImageUri);

这篇关于图片从画廊到imageview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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