从库中选择一个图像并将其显示在另一个活动中

时间:2014-10-03 08:08:15

标签: java android android-layout android-intent android-activity

我正在创建一个Android应用程序,我必须通过单击按钮从库中选择图像然后在另一个带有两个文本字段的活动中显示它,问题是我能够打开图库并从中选择图像但是我无法在其他活动中显示图像... 这是我的代码...... PictureOptions.java

public void buttonGalleryOpen(View view)
{
    Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, RESULT_LOAD_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Bitmap selectedphoto   = null;

     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 filePath = cursor.getString(columnIndex);
         selectedphoto = BitmapFactory.decodeFile(filePath);
         cursor.close();
         Intent intent = new Intent(PictureOptions.this,ShowImage.class);
         intent.putExtra("data", selectedphoto);
         startActivity(intent);
     }

PictureOptions.xml

<Button
    android:id="@+id/buttonGalleryOpen"
    android:layout_width="fill_parent"
    android:layout_height="66dp"
    android:layout_weight="0.34"
    android:onClick="buttonGalleryOpen"
    android:text="@string/button_gallery_open" />

ShowImage.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_image);
    ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
    Bitmap selectedphoto  =(Bitmap)this.getIntent().getParcelableExtra("data");
    imageview.setImageBitmap(selectedphoto);
}

ShowImage.xml

 <ImageView
    android:id="@+id/ImageShow"
    android:layout_width="200dp"
    android:layout_height="200dp" /> 

所有事情都运转正常,第二项活动(ShowImage)也在开放,除了没有任何关联......不知道为什么......?帮助

3 个答案:

答案 0 :(得分:1)

代码中的这一行没有意义:

intent.putExtra("data", "selectedphoto");

您在这里添加字符串“selectedphoto”,它与您之前初始化的selectedphoto变量无关。你可以将你的位图作为字节数组添加到额外的意图,但这是低效的,特别是当图像很大时。

不是将位图传递给ShowImage活动,而是传递您的URI,然后在ShowImage活动中检索实际位图,就像现在在PictureOptions活动中一样。

intent.setData( uri );

在ShowImage活动中执行:

URI imageUri = getIntent().getData();

答案 1 :(得分:0)

intent.putExtra("data", "selectedphoto");中有一个拼写错误,你传递的是String而不是位图。

中改变它
 Intent intent = new Intent(PictureOptions.this,ShowImage.class);
 intent.putExtra("data", selectedphoto);
 startActivity(intent);

selectedphoto

中删除双引号

答案 2 :(得分:0)

private void selectImage() {
    final CharSequence[] items = { "Photo Library", "Camera", "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Select");
    Utils.hideSoftKeyboard(getActivity());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Camera")) {

                  // camera intent

            } else if (items[item].equals("Photo Library")) {

                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);

            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {

          if (requestCode == SELECT_FILE) {
            Uri selectedImageUri = data.getData();

            String tempPath = getPath(selectedImageUri, getActivity());

            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bitmap = BitmapFactory.decodeFile(tempPath, btmapOptions);
            resized = Bitmap.createScaledBitmap(bitmap,
                    (int) (bitmap.getWidth() * 0.8),
                    (int) (bitmap.getHeight() * 0.8), true);

            profileEditImageView.setImageBitmap(resized);

     }
    }
   }


public String getPath(Uri uri, Activity activity) {
    String[] projection = { MediaColumns.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = activity
            .managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}