1
File
オブジェクトを取得しようとすると問題が発生しますが、画像からBitmap
を取得できます。私の質問は、onActivityResult
が返されたときに、どのようにしてFile
オブジェクトを得ることができるかです。ここでファイルオブジェクトを取得するときにonActivityResultを選択します。画像
は私のコードを行く:
画像に選択を開くには:
private void setUpAddMorePicturesClick() {
Button btnAddPictures = (Button)findViewById(R.id.btn_add_pictures);
btnAddPictures.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, getString(R.string.text_select_picture)),
ConstantValues.REQUEST_CODE_SELECT_IMAGE);
}
});
}
OnActivityResult方法:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ConstantValues.REQUEST_CODE_SELECT_IMAGE)
{
if (resultCode == Activity.RESULT_OK)
{
if (data != null)
{
//Uri uri = data.getData();
//String uriString = uri.toString();
//File imgFile = new File(uriString);
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();
File imgFile = new File(picturePath); // <-- NOT WORKING
// File imgFile = Environment.getExternalStoragePublicDirectory(data.getData().getPath());
//Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
//byte[] s = Utils.bitmapToByteArray(bitmap);
Snackbar snackbarError = Snackbar.make(rootRelativeLayout, R.string.msg_error_wcf_call_default, Snackbar.LENGTH_LONG);
//new EscortBusiness(this).addImage(s, snackbarError);
new EscortBusiness(this).addImageMultiPart(imgFile, snackbarError);
}
} else if (resultCode == Activity.RESULT_CANCELED)
{
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
Fileオブジェクトを取得しないでください。あなたはそれを必要としません。 uriを直接使用してください。 – greenapps
@greenapps実際に私はそれを必要とし、選択した画像をアップロードしなければならず、retrofitメソッドはFileオブジェクトをパラメータとして使用します。 – guisantogui
'// < - NOT WORKING'。悪いエラーの説明。どのパスを手に入れましたか?代わりに何が起こるのですか? – greenapps