Reputation: 1965
I am getting 0 values when I use this method. I thing the problem is in how I am creating the file. I want to be able to use the Uri from selecting a file via Intent.ACTION_GET_CONTENT. I think I am creating the file wrong but this won't work fully for local files or drive files. If someone could explain how to attack this problem of getting information from files not dependent on where they come from that is what I am looking how to do.
private FileData getFileDataFromUri(Uri uri){
//get file from uri
File file = new File(uri.getPath());
//initialize variables
FileData thisFileData = new FileData();
thisFileData.sizeInBytes = (int) file.length();
thisFileData.fileName = file.getName();
thisFileData.path = file.getPath();
byte[] bytes = new byte[thisFileData.sizeInBytes];
//get all bytes of file
try {
BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(file));
buffer.read(bytes, 0,bytes.length);
buffer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("getBinaryDataFromUri", e+"");
thisFileData.errorCode = 1;
return thisFileData;
} catch (IOException e) {
e.printStackTrace();
Log.e("getBinaryDataFromUri", e+"");
thisFileData.errorCode = 1;
return thisFileData;
}
//convert bytes array to string
thisFileData.bytes = new String(bytes);
//return
return thisFileData;
}
The class fileData is just the information from the file that I need do a lot of things with, here are its contents:
public class FileData {
int errorCode = 0;
int sizeInBytes = 0;
String fileName = "";
String bytes = "";
String path = "";
}
And here is the intent I originally called to get the uri that is sent to function getFileDataFromUri(Uri uri)
public void selectDocument() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT), chooser = null;
intent.setType("*/*"); //iOS uses "application/octet-stream"
chooser = Intent.createChooser(intent, "Find file...");
startActivityForResult(chooser, PICK_FILE_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_FILE_REQUEST){
if(resultCode == Activity.RESULT_OK){
Uri fullUri = data.getData();
selectPrinter(fullUri);
}
}
}
Upvotes: 0
Views: 581
Reputation: 1007296
I want to be able to use the Uri from selecting a file via Intent.ACTION_GET_CONTENT
A Uri
is not a file, any more than an HTTP URL is. Use ContentResolver
and openInputStream()
to read in the stream associated with a Uri
that you get from ACTION_GET_CONTENT
.
Upvotes: 2