从 phone 的内存中选择图片并将其设置为背景

Selecting picture from phone's memory and setting it as background

我已经重复了本网站答案中描述的方法,但我的应用程序无法正常运行。

当我按下 select 按钮时,新图像 window 打开,显示包含图片的所有文件夹。当我 select 只需点击一张图片时, select 用于 select 图片的 window 消失而不要求任何确认,并且我的应用程序中没有图像设置为背景。

我正在使用 Android Marshmallow 在 Nexus 5x 上对其进行测试。如果有任何帮助,我将不胜感激。

主要活动:

public class MainActivity extends AppCompatActivity {

private static int RESULT_LOAD_IMAGE = 1;

ImageView bgImage;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    btn = (Button) findViewById(R.id.changeButton);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 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};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        bgImage = (ImageView) findViewById(R.id.bgImage);
        bgImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}
}

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.marat.tutorialimageupload.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">

<ImageView
    android:id="@+id/bgImage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

<Button
    android:id="@+id/changeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Image"
    android:layout_weight="0"
    android:layout_marginBottom="20dp"
    android:layout_gravity="center"/>

</LinearLayout>

看看这一行:

if (requestCode == RESULT_LOAD_IMAGE && requestCode == RESULT_OK && null != data)

requestCode不能同时为RESULT_LOAD_IMAGERESULT_OK。将其更改为以下内容:

if (resultCode == RESULT_OK && requestCode == RESULT_LOAD_IMAGE && null != data)

您需要检查 RESULT_OKresultCode,而不是 requestCode。希望这能解决您遇到的问题。

编辑

好的。也许对您的代码进行这些修改可以有所帮助:

  1. 首先将此行 bgImage = (ImageView) findViewById(R.id.bgImage); 移动到您的 onCreate

  2. 接下来,在调用 startActivityForResult 之前设置意图类型,如下所示:intent.setType("image/*");

  3. 然后把onActivityResultif条件里面的代码改成这样:

            Uri selectedImageUri = data.getData();
            String[] filePathColumn = {MediaStore.MediaColumns.DATA};
            CursorLoader cursorLoader = new CursorLoader(this, selectedImageUri, filePathColumn, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            String picturePath = cursor.getString(columnIndex);
            Bitmap bm;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            bm = BitmapFactory.decodeFile(picturePath, options); 
            bgImage.setImageBitmap(bm);