Reputation: 21
I want to upload the file from mobile storage(both internal and external storage) to the server.By using a file chooser I got FileNotFoundException
. Here is the snippet of my code:
public void openFileSystem(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select file to upload."), SELECT_FILE);
}
Here is my onActivityResult()
and getPath()
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_FILE && resultCode == Activity.RESULT_OK) {
Uri path = data.getData();
String url = data.getData().getPath();
File file = new File(url);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor!=null)
{
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
The error log:
05-07 06:41:03.087: W/System.err(2195): java.io.FileNotFoundException: /document/2: open failed: ENOENT (No such file or directory)
05-07 06:41:03.097: W/System.err(2195): at libcore.io.IoBridge.open(IoBridge.java:409)
05-07 06:41:03.097: W/System.err(2195): at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-07 06:41:03.097: W/System.err(2195): at com.gems.ComposeBulletin.onActivityResult(ComposeBulletin.java:121)
05-07 06:41:03.117: W/System.err(2195): at android.app.Activity.dispatchActivityResult(Activity.java:5423)
05-07 06:41:03.117: W/System.err(2195): at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
05-07 06:41:03.127: W/System.err(2195): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
05-07 06:41:03.137: W/System.err(2195): at android.app.ActivityThread.access$1300(ActivityThread.java:135)
05-07 06:41:03.137: W/System.err(2195): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
05-07 06:41:03.137: W/System.err(2195): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 06:41:03.157: W/System.err(2195): at android.os.Looper.loop(Looper.java:136)
05-07 06:41:03.157: W/System.err(2195): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-07 06:41:03.167: W/System.err(2195): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 06:41:03.167: W/System.err(2195): at java.lang.reflect.Method.invoke(Method.java:515)
05-07 06:41:03.177: W/System.err(2195): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-07 06:41:03.187: W/System.err(2195): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-07 06:41:03.187: W/System.err(2195): at dalvik.system.NativeStart.main(Native Method)
05-07 06:41:03.197: W/System.err(2195): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-07 06:41:03.217: W/System.err(2195): at libcore.io.Posix.open(Native Method)
05-07 06:41:03.217: W/System.err(2195): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-07 06:41:03.227: W/System.err(2195): at libcore.io.IoBridge.open(IoBridge.java:393)
So my motto is to choose any file(image
, pdf
,doc
) from internal storage or external storage and upload it on the server. Any help will be appreciated!
Upvotes: 2
Views: 3306
Reputation: 13555
Try this...
Browse = (ImageView)findViewById(R.id.browsehoro);
Browse.setOnClickListener(this);
public void onClick(View v)
{
switch(v.getId())
{
case R.id.browsehoro:
img.setEnabled(true);
Intent intent = new Intent(Horoscope.this, FilePickerHoroscope.class);
startActivityForResult(intent, REQUEST_PICK_FILE);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
/* if (requestCode == REQUEST_PICK_FILE) {
Uri selectedImageUri = data.getData();
selectedFile = getPath(selectedImageUri);
// Print imagepath in textview here
filePath.setText(selectedFile);
System.out.println(requestCode);
System.out.println("Image Path : " + selectedFile);
img.setImageURI(selectedImageUri);
}*/
switch(requestCode)
{
case REQUEST_PICK_FILE:
if(data.hasExtra(FilePickerHoroscope.EXTRA_FILE_PATH))
{
selectedFile = new String
(data.getStringExtra(FilePickerHoroscope.EXTRA_FILE_PATH));
filePath.setText(selectedFile.toString());
}
break;
}
}
}
Upvotes: 0
Reputation: 1006869
That is not reliable with ACTION_GET_CONTENT
, as there is no requirement that the Uri
get back point to a file that you can access. The file might be on internal storage of an app, or on removable media, or even in the cloud somewhere. For what feels like the sixth time this week and definitely the second within the past ten minutes... a Uri
is not a file.
Use the Uri
as designed, via methods on ContentResolver
like openInputStream()
and getType()
.
Upvotes: 3
Reputation: 11224
String url = data.getData().getPath();
File file = new File(url);
The String url does contain a content provider path. In this case as you can see in the logcat /document/2
. The File
class cannot handle such content provider paths.
You first have to convert that path to a real file system path.
Google for 'getRealPathFromUri'.
Upvotes: -1