web-dev-qa-db-ja.com

ギャラリーから画像ビューに画像を読み込む方法は?

ボタンがあるアクティビティがあります。ボタンをクリックすると、画像ギャラリーにリダイレクトされます。画像ビューを使用して、次のアクティビティで選択した画像を表示したい。ただし、画像は表示されません。画像が設定されると、ビューは画面外になります。

画像を選択して次に進むための私のコードを以下に示します。私は自分の活動に真実の歴史を使用していません。

  @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();
        if (!(picturePath.equals(""))) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, ImageInGellary.class);
            intent.putExtra("picturePath", picturePath);
            startActivity(intent);

        }
    }
}

public class ImageInGellary extends Activity {
    Button cancel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.load_image);
        cancel = (Button) findViewById(R.id.buttonCancelPicture);
        Intent in = getIntent();
        savedInstanceState = in.getExtras();
        String picturePath = savedInstanceState.getString("picturePath");
        ImageView imageView = (ImageView) findViewById(R.id.img_view);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        cancel.setOnClickListener(new OnClickListener() {

            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);
                 */
                Intent intent = new Intent();
                intent.setClass(ImageInGellary.this, MainActivity.class);
                startActivity(intent);

            }
        });
    }

}
8
Nitesh Kabra
    public class ImageGalleryDemoActivity extends Activity {


        private static int RESULT_LOAD_IMAGE = 1;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
            buttonLoadImage.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

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

                ImageView imageView = (ImageView) findViewById(R.id.imgView);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }


        }
    }
19
Amit Prajapati

あなたが1行でそれを達成したいなら、あなたがする必要があるすべては次のとおりです:

Picasso.with(MainActivity.this).load(data.getData()).noPlaceholder().centerCrop().fit().into((ImageView) findViewById(R.id.imageView1));

完全なソリューション:-

画像を選択

public void pickImage() {

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

}

画像を読み込む

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

            //Get ImageURi and load with help of picasso
            //Uri selectedImageURI = data.getData();

            Picasso.with(MainActivity1.this).load(data.getData()).noPlaceholder().centerCrop().fit()
                    .into((ImageView) findViewById(R.id.imageView1));
        }

    }
}

あなたの状況では、ImageURIを次のアクティビティに渡す必要があります

Uri selectedImageURI = data.getData();
8
Hitesh Sahu
public void onClick(View view) {
    Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
    gallery.setType("image/*");
    startActivityForResult(gallery, 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){
        Uri imageUri = data.getData();
        imgView.setImageURI(imageUri);

    }
}
5
Adhi Sumitro

何も機能しない場合は、次のコードが機能します。

import Java.io.FileDescriptor;
import Java.io.IOException;
import Android.app.Activity;
import Android.content.Intent;
import Android.database.Cursor;
import Android.graphics.Bitmap;
import Android.graphics.BitmapFactory;
import Android.net.Uri;
import Android.os.Bundle;
import Android.os.ParcelFileDescriptor;
import Android.provider.MediaStore;
import Android.view.View;
import Android.widget.Button;
import Android.widget.ImageView;

public class ImageGalleryDemoActivity extends Activity {


    private static int RESULT_LOAD_IMAGE = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

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

            ImageView imageView = (ImageView) findViewById(R.id.imgView);

            Bitmap bmp = null;
            try {
                bmp = getBitmapFromUri(selectedImage);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            imageView.setImageBitmap(bmp);

        }


    }



    private Bitmap getBitmapFromUri(Uri uri) throws IOException {
        ParcelFileDescriptor parcelFileDescriptor =
                getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
    }

}

私はあなたの問題を解決しました。画像が選択されたら、画像のUriを文字列に変換し、2番目のクラスで文字列を画像に変換します。

http://whats-online.info/science-and-tutorials/95/Android-get-image-from-gallery-into-imageview-programmatically/

http://whats-online.info/science-and-tutorials/49/Android-passing-multiple-data-from-one-activity-to-another/

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case SELECT_PHOTO:
                if(resultCode == RESULT_OK){
                    Uri selectedImage = data.getData();
                    if(selectedImage !=null){
                        Intent intent=new Intent(MainActivity.this, Activity2.class);
                  Bundle extras = new Bundle();
                  extras.putString("image", selectedImage.toString());
                  intent.putExtras(extras);
                  startActivity(intent);

                    }
                }
        }

Activity2.class

  Bundle extras = getIntent().getExtras();
          String name = extras.getString("image");
         Uri imageFinal=Uri.parse(name);
1
Daniel Nyamasyo