从手机存储器中选择照片并将其设置为背景

时间:2016-06-05 11:58:22

标签: android android-imageview

我重复了本网站上的答案所描述的方法,但我的应用程序无法正常工作。

当我按下按钮选择图像时,将打开一个新窗口,其中显示包含图片的所有文件夹。当我通过点击它选择一个图像时,用于选择图片的窗口消失而不需要任何确认,并且我的应用程序中没有图像被设置为背景。

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

MainActivity:

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>

1 个答案:

答案 0 :(得分:1)

看看这一行:

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

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

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

传递到resultCode的{​​{1}}需要检查onActivityResult,而不是RESULT_OK。希望这能解决你遇到的问题。

修改

好。也许对代码的这些修改可以提供帮助:

  1. 首先将此行requestCode移至bgImage = (ImageView) findViewById(R.id.bgImage);

  2. 接下来,在调用onCreate之前设置意图类型,如下所示:startActivityForResult

  3. 然后将intent.setType("image/*");if条件中的代码更改为:

    onActivityResult