How to set selected image from SD card on imageview in Android

本秂侑毒 提交于 2020-01-15 11:27:06

问题


I am a beginner of android development. I am trying to develop my own gallery. But I am facing a problem. When I go to SD card on my memory and choose a photo then I am trying to open image using my Image Viewer app. But when I select Image Viewer then another intent is called for choosing my image. But I directly want show the selected image on Imageview. Please anyone help me.

My code:

public class MainActivity extends Activity {

     private static final int REQUEST_CODE = 1;
      private Bitmap bitmap;
      private ImageView imageView;
      private String selectedImagePath;


      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.result);
        pickImage();
      }

      public void pickImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_CODE);
      }

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


        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
          try {
            // recyle unused bitmaps

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

                    Log.v("roni", filePath);
                    cursor.close();

                    if(bitmap != null && !bitmap.isRecycled())
                    {
                        bitmap = null;                
                    }

                    bitmap = BitmapFactory.decodeFile(filePath);
                    imageView.setBackgroundResource(0);
                    imageView.setImageBitmap(bitmap);              


           // imageView.setImageBitmap(bitmap);
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            if (stream != null)
              try {
                stream.close();
              } catch (IOException e) {
                e.printStackTrace();
              }
          }
      }
      private String getPath(Uri uri)
      {    
      String[] projection={MediaStore.Images.Media.DATA}; 
      Cursor cursor=managedQuery(uri,projection,null,null,null);
      int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
  }  
}

/////After changing the code

public class MainActivity extends Activity {

 private static final int REQUEST_CODE = 1;
  private Bitmap bitmap;
  private ImageView imageView;
  private String selectedImagePath;


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.result);

    Intent i = new Intent(
            Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, REQUEST_CODE);


  }



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


    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
      try {
        // recyle unused bitmaps

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

                Log.v("roni", filePath);
                cursor.close();

                if(bitmap != null && !bitmap.isRecycled())
                {
                    bitmap = null;                
                }

                bitmap = BitmapFactory.decodeFile(filePath);
                //imageView.setBackgroundResource(0);
                imageView.setImageBitmap(bitmap);              


       // imageView.setImageBitmap(bitmap);
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (stream != null)
          try {
            stream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
      }
  }


  private String getPath(Uri uri)
  {    
  String[] projection={MediaStore.Images.Media.DATA}; 
  Cursor cursor=managedQuery(uri,projection,null,null,null);
  int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
  cursor.moveToFirst(); 
  return cursor.getString(column_index); }  }

回答1:


Let's assume you have an activity called ViewActivity that shows selected Image.

in your AndroidManifest.xml set your activity as a Image Viewer Activity by adding this :

<activity android:name=".ViewActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="image/*"/>
    </intent-filter>
</activity>

in onCreate() method of ViewActivity catch passed Image by doing this :

Intent intent = getIntent();
Uri data = intent.getData();
//Check If data type is Image
if (intent.getType().indexOf("image/") != -1)
{
    myImageView.setImageURI(data);
}



回答2:


try this Code replace whole class plz if success then i will explain to you

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

        }


    }
}


来源:https://stackoverflow.com/questions/25821239/how-to-set-selected-image-from-sd-card-on-imageview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!