我创建了一个应用程序,允许我从图库中拍照。这是我的代码......
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"));
}
}
}
问题是......我如何将onIctivityResult中的selectedImagePath传递给onClick函数?我想在这里传递我的selectedImagePath
File selectedImage = new File("/storage/emulated/0/DCIM/Camera/IMG_20160316_051845.jpg");
//图像路径
答案 0 :(得分:0)
尝试将selectedImagePath声明为全局最终字符串。我猜这样可行。
我会将此作为评论添加,但我的声誉是<50。
答案 1 :(得分:0)
在任何方法之外声明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"));
}