How to pick an image from gallery (SD Card) for my app?

前端 未结 10 1868
Happy的楠姐
Happy的楠姐 2020-11-21 23:40

This question was originally asked for Android 1.6.

I am working on photos options in my app.

I have a button and an ImageView in my Activity. When I click

10条回答
  •  执笔经年
    2020-11-22 00:13

    public class EMView extends Activity {
    ImageView img,img1;
    int column_index;
      Intent intent=null;
    // Declare our Views, so we can access them later
    String logo,imagePath,Logo;
    Cursor cursor;
    //YOU CAN EDIT THIS TO WHATEVER YOU WANT
    private static final int SELECT_PICTURE = 1;
    
     String selectedImagePath;
    //ADDED
     String filemanagerstring;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        img= (ImageView)findViewById(R.id.gimg1);
    
    
    
        ((Button) findViewById(R.id.Button01))
        .setOnClickListener(new OnClickListener() {
    
            public void onClick(View arg0) {
    
                // in onCreate or any event where your want the user to
                // select a file
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Select Picture"), SELECT_PICTURE);
    
    
            }
        });
    }
    
    //UPDATED
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
    
                //OI FILE Manager
                filemanagerstring = selectedImageUri.getPath();
    
                //MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);
    
    
                img.setImageURI(selectedImageUri);
    
               imagePath.getBytes();
               TextView txt = (TextView)findViewById(R.id.title);
               txt.setText(imagePath.toString());
    
    
               Bitmap bm = BitmapFactory.decodeFile(imagePath);
    
              // img1.setImageBitmap(bm);
    
    
    
            }
    
        }
    
    }
    
    //UPDATED!
    public String getPath(Uri uri) {
    String[] projection = { MediaColumns.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    column_index = cursor
            .getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
     imagePath = cursor.getString(column_index);
    
    return cursor.getString(column_index);
    }
    
    }
    

提交回复
热议问题