簡體   English   中英

將圖像路徑從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聲明為全局最終字符串。

我會將其添加為評論,但我的聲譽是<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"));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM