简体   繁体   English

从图库中选择图像,然后在另一个活动中显示

[英]Select a image from the gallery and show it in another Activity

I am making an android application in which i have to select image from gallery by clicking a button and then display it in another activity with two text fields, the problem is i am able to open the gallery and select image from it but i am not able to display image in another activity... here is my code... PictureOptions.java 我正在制作一个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" /> 

All things are working fine and second activity(ShowImage) is also opening except that no iamge is displying....dont know why..?HELP 一切正常,并且第二个活动(ShowImage)也正在打开,除了没有iamge在显示....不知道为什么..?HELP

This line in your code does not make sense: 您的代码中的这一行没有意义:

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

You are adding here string "selectedphoto" which is in no way connected to selectedphoto variable you initialised earlier. 您将在此处添加字符串“ selectedphoto”,该字符串绝不会连接到您先前初始化的selectedphoto变量。 You could put your bitmap to intent extra as byte array but this is inefficient, especially when the image is large. 您可以将位图作为字节数组额外使用,但这效率低下,尤其是在图像较大时。

Instead of passing bitmap to ShowImage activity, pass your URI and then retrieve actual bitmap in ShowImage activity exactly as you do now in your PictureOptions activity. 而不是将位图传递给ShowImage活动,而是传递URI,然后完全像现在在PictureOptions活动中那样在ShowImage活动中检索实际位图。

intent.setData( uri );

In your ShowImage activity do: 在您的ShowImage活动中,请执行以下操作:

URI imageUri = getIntent().getData();

Yo have a typo in intent.putExtra("data", "selectedphoto"); intent.putExtra("data", "selectedphoto");有错字intent.putExtra("data", "selectedphoto"); , you are passing a String not the bitmap. ,您传递的是String而不是位图。 Change it in 改变它

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

removing the double quote from selectedphoto selectedphoto删除双引号

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM