0

I am having problem with this Line of Code.

entity.addPart("file", new FileBody(new File(loadedData.getData().getPath())));

This is a part of it.

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("file", new FileBody(new File(loadedData.getData().getPath())));

I am not getting any error that I am doing something wrong here but in the log cat.

07-19 16:09:12.424      140-493/? E/﹕ AudioCloseDumpPCMFile file== NULL

I am getting this

If I comment out the addpart for file. The rest of my entity part goes no problem and I was able to push it through. Problem is the file. I have loaded the loadedData from an Intent Image picker and that is what I want to include.

Update

/document/image:11018 is what I am getting from loadedData.getData().getPath()

I have tried to create a new File instead of inserting it into FileBody like this

File file = loadedData.getData().getPath() 

Now I am getting invalid type. As it requires java.io.file and of course I am sending a String.

So I am really not familiar turning an Intent to a file data.

Aizen
  • 1,807
  • 2
  • 14
  • 28
  • 'loadedData.getData().getPath()'. Please tell the contents of this string. How can we know if you don't tell us? – greenapps Jul 19 '15 at 08:24
  • I have updated my question. Thanks for viewing. – Aizen Jul 19 '15 at 08:32
  • You should have tried File file = new File( loadedData.getData().getPath() ); instead. And then use if ( file.exists()) { ok lets add it } else { ohhh no file}. You will see that /document/image:11018 is no path to a file on the file system but a content provider path. The File class cannot handle such paths. – greenapps Jul 19 '15 at 08:37
  • Thanks, I have tried new File etc. But I will then add if file.exists thanks. How can I handle the document/image ? It just hits me that this is not the real path? – Aizen Jul 19 '15 at 08:41
  • `It just hits me`. It hits everyone. Often you can convert such a content provider path to a file system path though. But i'm not gonna tell you that you have to google for getrealpathfrom uri as @CommonsWare will curse me even more then. Instead i advise you to look if it is possible to add a FileBody from a FileInputSTream. If so then you can use instead of FileInputStream getContentResolver().getStream(...) (or something like that). – greenapps Jul 19 '15 at 08:55
  • Thanks for the Advice, currently reading this. `http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework` – Aizen Jul 19 '15 at 09:10
  • I forgot to mention I am using KitKat. and I am not so sure before that it makes a difference until now. Seems they have change the getrealpath location. – Aizen Jul 19 '15 at 09:11

1 Answers1

0
public String getRealPathFromURI(Context context, Uri contentUri) {
 Cursor cursor = null;
  try { 
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
  if (cursor != null) {
  cursor.close();
   }
  }
 }

This Method solved my problem found the answer in here. Solution in here

Community
  • 1
  • 1
Aizen
  • 1,807
  • 2
  • 14
  • 28