Picture from gallery to an imageview

怎甘沉沦 提交于 2019-12-13 04:35:30

问题


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

Here's the code:

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");
            }
         }
    });
}

Here's the layout:

<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>

and here's the MANIFEST:

<?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


回答1:


Got the bug in your code:

String filePath = null;

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);


来源:https://stackoverflow.com/questions/22238015/picture-from-gallery-to-an-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!