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

前端 未结 10 1884
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:15

    #initialize in main activity 
        path = Environment.getExternalStorageDirectory()
                + "/images/make_machine_example.jpg"; #
         ImageView image=(ImageView)findViewById(R.id.image);
     //--------------------------------------------------||
    
     public void FromCamera(View) {
    
        Log.i("camera", "startCameraActivity()");
        File file = new File(path);
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 1);
    
    }
    
    public void FromCard() {
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, 2);
    }
    
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == 2 && 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();
    
            bitmap = BitmapFactory.decodeFile(picturePath);
            image.setImageBitmap(bitmap);
    
            if (bitmap != null) {
                ImageView rotate = (ImageView) findViewById(R.id.rotate);
    
            }
    
        } else {
    
            Log.i("SonaSys", "resultCode: " + resultCode);
            switch (resultCode) {
            case 0:
                Log.i("SonaSys", "User cancelled");
                break;
            case -1:
                onPhotoTaken();
                break;
    
            }
    
        }
    
    }
    
    protected void onPhotoTaken() {
        // Log message
        Log.i("SonaSys", "onPhotoTaken");
        taken = true;
        imgCapFlag = true;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        bitmap = BitmapFactory.decodeFile(path, options);
        image.setImageBitmap(bitmap);
    
    
    }
    

提交回复
热议问题