简体   繁体   中英

Passing image path from onActivityResult to onClick function

I have created an app that allows me to take picture from the gallery. Here is my code...

    public class PhotoActivity extends AppCompatActivity {

    Button btnCamera, btnShare, btnGallery;
    ImageView iv;
    private static final int CAM_REQUEST = 1;
    private static final int SELECT_FILE = 2;
    private File imageFile;
    private String selectedImagePath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);

        btnCamera = (Button) findViewById(R.id.buttonCamera);
        btnShare = (Button) findViewById(R.id.buttonShare);
        btnGallery = (Button) findViewById(R.id.buttonGallery);
        iv = (ImageView) findViewById(R.id.imageView);
        btnCamera.setOnClickListener(new btnCameraClicker());
        btnShare.setOnClickListener(new btnShareClicker());
        btnGallery.setOnClickListener(new btnGalleryClicker());

        //set button to false is camera isn't used
        btnShare.setEnabled(false);


    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("onActivityResult", "");

        if (requestCode == CAM_REQUEST && resultCode == RESULT_OK )
        {
            galleryAddPic();
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
//            bitmapOptions.inJustDecodeBounds = true;
            //imageFile.getAbsolutePath()
            Bitmap bMap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), bitmapOptions);
            iv.setImageBitmap(bMap);

//            Log.d("Image File Path:", imageFile.getAbsolutePath());

//            Toast.makeText(this, "bMap:" + bMap, Toast.LENGTH_LONG).show();
            Toast.makeText(this, "saved" + imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();

        }
        else if(requestCode == SELECT_FILE && resultCode == RESULT_OK)
        {
            Uri selectedImageUri = data.getData();
            String[] projection = { MediaStore.MediaColumns.DATA };

            CursorLoader cursorLoader = new CursorLoader(this,selectedImageUri, projection, null, null,
                    null);
            Cursor cursor =cursorLoader.loadInBackground();
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            String selectedImagePath = cursor.getString(column_index);


            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            Bitmap bMap = BitmapFactory.decodeFile(selectedImagePath, bitmapOptions);
            iv.setImageBitmap(bMap);
            btnShare.setEnabled(true);

        }
    }

Then I created a button listener which enables user to share the image from the gallery to other app.

    class btnShareClicker implements View.OnClickListener {
        public void onClick(View v) {
            if(imageFile!=null)
            {
                Uri imagePath = Uri.fromFile(imageFile);
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                sharingIntent.setType("image/*");
                sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
                startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));
            }
            else
            {
                File selectedImage = new File("/storage/emulated/0/DCIM/Camera/IMG_20160316_051845.jpg");
//                Uri bmpUri = getLocalBitmapUri(iv);
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                sharingIntent.setType("image/*");
                sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(selectedImage));
                startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));
            }
        }
    }

The question is... how do i passed the selectedImagePath from onActivityResult to onClick function? I wanted to pass my selectedImagePath in here

File selectedImage = new File("/storage/emulated/0/DCIM/Camera/IMG_20160316_051845.jpg");

//the image path

Try to declare the selectedImagePath as a global final string.I guess that would work.

I would have added this as a comment but my reputation is <50.

Declare selectedImagePath outside any method as a member variable.

private String selectedImagePath;

Then inside onActivityResult() save the filepath

selectedImagePath = cursor.getString(column_index);

Inside your onClick(),

if(selectedImagePath == null){
//Show error message

} else{
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                    sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                    sharingIntent.setType("image/*");
                    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(selectedImagePath));
                    startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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