将图像路径从 onActivityResult 传递给 onClick 函数

Passing image path from onActivityResult to onClick function

我创建了一个允许我从画廊拍照的应用程序。这是我的代码...

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

        }
    }

然后我创建了一个按钮侦听器,使用户能够将图库中的图像共享到其他应用程序。

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

问题是...如何将 selectedImagePath 从 onActivityResult 传递给 onClick 函数?我想在这里传递我的 selectedImagePath

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

//图片路径

尝试将 selectedImagePath 声明为全局最终 string.I 猜测可行。

我会把它添加为评论,但我的声誉 <50。

在任何方法之外声明selectedImagePath作为成员变量。

private String selectedImagePath;

然后在onActivityResult()里面保存文件路径

selectedImagePath = cursor.getString(column_index);

在你的 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"));
}