2017-01-15 6 views
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(); 
     } 
    } 
} 
+0

Fileオブジェクトを取得しないでください。あなたはそれを必要としません。 uriを直接使用してください。 – greenapps

+0

@greenapps実際に私はそれを必要とし、選択した画像をアップロードしなければならず、retrofitメソッドはFileオブジェクトをパラメータとして使用します。 – guisantogui

+0

'// < - NOT WORKING'。悪いエラーの説明。どのパスを手に入れましたか?代わりに何が起こるのですか? – greenapps

答えて

0

そのスキーマがファイルであってもよいが発生URIからInputStreamを使用します://または内容://

BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())); 
関連する問題