I am working on project were i have to upload PDF file or DOC file i have write too much codes but no success
whenever i clicked submit Toast say Upload PDF File and Activity exit.
how to solve the problem?
here is the Code i use
public class contactus extends AppCompatActivity {
EditText name,email,mobile;
Button submit,uploadCv;
public static final String pdfurl = "https://umsctest.000webhostapp.com/testdb/upload.php";
Uri uri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contactus);
name = findViewById(R.id.et_name);
email = findViewById(R.id.et_email);
mobile = findViewById(R.id.et_no);
submit = findViewById(R.id.bt_submit);
uploadCv = findViewById(R.id.bt_uploadCv);
InputFilter[] inputFilter = new InputFilter[1];
inputFilter[0] = new InputFilter.LengthFilter(10);
mobile.setFilters(inputFilter);
uploadCv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
uploadfile();
}
});
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String fname = name.getText().toString();
String mail = email.getText().toString();
String mob = mobile.getText().toString();
String filepath = FilePath.getPath(contactus.this,uri);
if (filepath == null)
{
Toast.makeText(getApplicationContext(),"Please Upload PDF file",Toast.LENGTH_SHORT).show();
}else
{
String pdfId = UUID.randomUUID().toString();
try {
new MultipartUploadRequest(contactus.this,pdfId,pdfurl).addFileToUpload(filepath, "pdf")
.addParameter("name",fname)
.addParameter("email",mail)
.addParameter("mobile",mob)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(5)
.startUpload();
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),"Error in Uploading Fis",Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (MalformedURLException e) {
Toast.makeText(getApplicationContext(),"Error in Uploading File",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
if (fname.length() <=0)
{
name.setError("Name Please");
}else if (mail.length() <=0)
{
email.setError("Email Please");
}else if (mob.length() <=0)
{
mobile.setError("WhatsApp No. Please");
}else if (mob.length() >10)
{
mobile.setError("Only 10 Digits No.");
}else
{
name.setText("");
email.setText("");
mobile.setText("");
}
}
});
}
private void uploadfile() {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(intent.createChooser(intent,"Select Pdf"),1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data !=null)
{
Uri path = data.getData();
uploadCv.setText("PDF Selected");
}
}
this is my contactus(mainactivity).java
and i also used FilePath.java
public class FilePath {
public static String getPath(final Context context, final Uri uri)
{
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri))
{
if (isExternalStorageDocument(uri))
{
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
return Environment.getExternalStorageDirectory() + "/" + split[1];
}else if (isDownloadsDocument(uri))
{
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}else if (isMediaDocument(uri))
{
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type))
{
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}else if("video".equals(type))
{
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}else if ("audio".equals(type))
{
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {split[1]};
}else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
}
return null;
}
private static String getDataColumn(Context context, Uri contentUri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(contentUri, projection, selection, selectionArgs,null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri)
{
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
Help me out its returns Null path
Related
This question already has answers here:
Android Kotlin: Getting a FileNotFoundException with filename chosen from file picker?
(5 answers)
Android - Get real path of a .txt file selected from the file explorer
(1 answer)
Closed 1 year ago.
I want to get absolute path by Uri, when picking some files from Downloads folder. API level 21+.
Currently I have such errors:
Input Uri: content://com.android.providers.downloads.documents/document/1603
Path: content://downloads/public_downloads/1603
Giving error: java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/1603
or this one:
java.io.FileNotFoundException: /document/msf:64020: open failed: ENOENT (No such file or directory)
java.lang.NumberFormatException: For input string: "msf:64020"
depending on how I access such files. How can I do that properly? Maby you can provide some usuful link to documentation. Thanks.
This is my current solution:
private FileUtils() {
}
static final String TAG = "FileUtils";
public static final String MIME_TYPE_AUDIO = "audio/*";
public static final String MIME_TYPE_TEXT = "text/*";
public static final String MIME_TYPE_IMAGE = "image/*";
public static final String MIME_TYPE_VIDEO = "video/*";
public static final String MIME_TYPE_APP = "application/*";
public static final String HIDDEN_PREFIX = ".";
public static String getExtension(String uri) {
if (uri == null) {
return null;
}
int dot = uri.lastIndexOf(".");
if (dot >= 0) {
return uri.substring(dot);
} else {
// No extension.
return "";
}
}
public static boolean isLocal(String url) {
return url != null && !url.startsWith("http://") && !url.startsWith("https://");
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static String getPath(final Context context, final Uri uri) {
// DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider ----- This if-else is working wrong.
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.parseLong(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static File getFile(Context context, Uri uri) {
if (uri != null) {
String path = getPath(context, uri);
if (!Strings.isNullOrEmpty(path) && isLocal(path)) {
return new File(path);
}
}
return null;
}
public static Intent createGetContentIntent(String mimeType) {
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType(mimeType);
// Only return URIs that can be opened with ContentResolver
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
return intent;
}
}
And this is in myActivity:
public void checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
IMAGE_PICKER_PERMISSION_KEY);
} else {
startImagePickerActivity();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == IMAGE_PICKER_PERMISSION_KEY && grantResults.length == 2
&& grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
startImagePickerActivity();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null) {
if (requestCode == REQUEST_CODE_CHOOSE) {
ClipData clipData = data.getClipData();
if (clipData != null) {
//user choose 2+ images
int i = 0;
while (i < clipData.getItemCount()) {
onPhotoAdded(clipData.getItemAt(i).getUri());
i++;
}
} else if (data.getData() != null) {
//user choose only 1 image
onPhotoAdded(data.getData());
}
}
}
}
public void onPhotoAdded(Uri uri) {
try {
File f = FileUtils.getFile(this, uri);
if (f == null) {
Log.d("LOG", "File is null");
} else {
listOfUri.add(uri);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.d("LOG", e.getMessage());
}
}
I use this code to pick an image from the device but when the activity is closed the image disappears. How to save it so whenever I open the Activity the image is still there?
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (resultCode == RESULT_OK && data!= null) {
Uri imageData = data.getData() ;
imageView.setImageURI(imageData);
}
}
}
So what you need is save the selected imageUri in local preferences of the activity.
Please check the code below:
public class Prefs {
private static Prefs INSTANCE;
private static SharedPreferences prefs;
public static Prefs getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = new Prefs();
prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
}
return INSTANCE;
}
public void setImage(String image) {
prefs.edit().putString("image", image).apply();
}
public String getImage() {
return prefs.getString("image", "");
}
Then in your activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
if(!TextUtils.isEmpty(Prefs.getInstance(applicationContext).getImage()){
val imgFile = File(Prefs.getInstance(getApl).getImage())
if (imgFile.exists()) {
val myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath())
imageView.setImageBitmap(myBitmap)
}
}
In your onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (resultCode == RESULT_OK && data!= null) {
Uri imageData = data.getData() ;
imageView.setImageURI(imageData);
Prefs.getInstance(applicationContext).setImage(PathUtils.getPath(applicationContext,imageData));
}
}
}
public class PathUtils {
public static String getPath(final Context context, final Uri uri) {
// DocumentProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
String storageDefinition;
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
if (Environment.isExternalStorageRemovable()) {
storageDefinition = "EXTERNAL_STORAGE";
} else {
storageDefinition = "SECONDARY_STORAGE";
}
return System.getenv(storageDefinition) + "/" + split[1];
}
} else if (isDownloadsDocument(uri)) {// DownloadsProvider
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {// MediaProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore (and general)
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
first, you must use cursor for catch media path, then you can store on the hawk or shared preferences with a special key. then open the image with data stored in Hawk or shared preferences.
eg :
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]);
mediaPath = cursor.getString(columnIndex);
Hawk.put("image",mediaPath);
//after_reset =>
String mediaPath1 = Hawk.get("image").toString();
channelImage.setImageBitmap(BitmapFactory.decodeFile(mediaPath1));
I am New in Android, Working on one functionality in which I have to pick up a file from internal storage and SDCARD. I have implemented some code. With the help of below code, I am able to pick up or select a file from SDcard and internal storage in the Android pie version(9 API level 28) and Android Nougat version(7) but I am not able to get pdf file in from SDCARD in Android Oreo(8 api level 26) version and for Marshmallow it is unable to select file from internal as well as SDCARD. I am not able to figure out what is the problem. Same code working for one not working for another. There is any universal way to access files(pdf and images).Thanks in advance
Implemented Code Below
private void SelectFileFromGallery(){
Intent intent = new Intent();
String[] mimeTypes = {"image/*", "application/pdf"};
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_file)), SELECT_FILE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE) {
onSelectFileFromGalleryResult(data);
isUpdated = true;
} else if (requestCode == 11) {
onBackPressed();
}
}
private void onSelectFileFromGalleryResult(Intent data) {
try {
InputStream is;
Uri selectedImageUri = data.getData();
String file= AndroidUtilities.getFilePath(selectedImageUri);
File imageFile = new File(file);
Log.d("File Path",imageFile.getAbsolutePath());
if (imageFile.exists()) {
// Here I am Compressing the file to store
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getFilePath(final Uri uri) {
String filePath="";
try {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(MyApp.applicationContext, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type=split[0];
String storageDefinition;
// if ("primary".equalsIgnoreCase(type)) {
// return Environment.getExternalStorageDirectory() + "/" + split[1];
// } else {
//
// if(Environment.isExternalStorageRemovable()){
// storageDefinition = "EXTERNAL_STORAGE";
//
// } else{
// storageDefinition = "SECONDARY_STORAGE";
// }
//
// return System.getenv(storageDefinition) + "/" + split[1];
// }
if("primary".equalsIgnoreCase(type)){
if (split.length > 1) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
return Environment.getExternalStorageDirectory() + "/";
}
// This is for checking SD Card
}
else if ("home".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/documents/" + split[1];
}
else {
return "storage" + "/" + docId.replace(":", "/");
}
} else if (isDownloadsDocument(uri)) {
String fileName = getFileName(MyApp.applicationContext, uri);
final String id = DocumentsContract.getDocumentId(uri);
if(fileName!=null){
return Environment.getExternalStorageDirectory().toString()+"/Download/"+fileName;
}
if (id != null && id.startsWith("raw:")) {
return id.substring(4);
}
String[] contentUriPrefixesToTry = new String[]{
"content://downloads/public_downloads",
"content://downloads/my_downloads",
"content://downloads/all_downloads",
};
// final Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefixesToTry), Long.valueOf(id));
// return getDataColumn(MyApp.applicationContext, contentUri, null, null);
for (String contentUriPrefix : contentUriPrefixesToTry) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));
try {
String path = getDataColumn(MyApp.applicationContext, contentUri, null, null);
if (path != null) {
return path;
}
} catch (Exception e) {}
}
// File cacheDir = getDocumentCacheDir(MyApp.applicationContext);
// File file = generateFileName(fileName, cacheDir);
// String destinationPath = null;
// if (file != null) {
// destinationPath = file.getAbsolutePath();
// saveFileFromUri(MyApp.applicationContext, uri, destinationPath);
// }
// return destinationPath;
} else if (isMediaDocument(uri)||isMediaStorage(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
switch (type) {
case "image":
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
break;
case "video":
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
break;
case "audio":
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
break;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(MyApp.applicationContext, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(MyApp.applicationContext, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
} catch (Exception e) {
Log.e("Exception", e.toString());
}
return null;
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {MediaStore.Images.Media.DATA};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String value = cursor.getString(column_index);
if (value.startsWith("content://") || !value.startsWith("/") && !value.startsWith("file://")) {
return null;
}
return value;
}
} catch (Exception e) {
Log.e("Exception", e.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private static boolean isMediaStorage(Uri uri) {
return "com.google.android.apps.docs.storage".equals(uri.getAuthority());
}
i'am trying to make a finder to get '.pdf', i got my Uri the file exist but he always return false for File.canRead()
Just before trying to upload that document on my parse server i log this:
final File file = new File(path);
Log.d("=== TAG ====", file.getAbsolutePath());
Log.d("=== TAG ====", String.valueOf(file.exists()));
Log.d("=== TAG ====", String.valueOf(file.canRead()));
Give me this:
06-27 10:58:03.746 24136-24136/bruce.team D/=== TAG ====: /storage/emulated/0/Download/random.pdf
06-27 10:58:03.747 24136-24136/bruce.team D/=== TAG ====: true
06-27 10:58:03.747 24136-24136/bruce.team D/=== TAG ====: false
Then it's just fail uploading after 15 sec, my parse-server isn't access so it must be the read problem !
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
My way to pick it:
public static void pick(final Controller controller) {
final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
chooseFileIntent.setType("application/pdf");
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (chooseFileIntent.resolveActivity(controller.getContext().getPackageManager()) != null) {
controller.startActivityForResult(chooseFileIntent, Configuration.Request.Code.Pdf.Pdf);
}
}
So my question why canRead always return false ? How can I read it?
EDIT: The way I get my Path that seems good for me.
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static String getPath(Context context, Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else
if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {split[1]};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
My on activity result :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Configuration.Request.Code.Pdf.Pdf) {
if (resultCode == RESULT_OK) {
Uri fileUri = data.getData();
if (Session.User != null) {
final Document document = new Document(Session.User);
document.setCategory(Document.Category.CurriculumVitae);
document.setType("PDF");
document.save(getContext(), fileUri, new Uploader.Handler() {
#Override
public void progress(Integer percentage) {
Log.d("=====", String.valueOf(percentage));
if (isLoading()) {
int progressValue = 0;
progressValue += document.uploaded;
documentProgressBar.setProgress(60 * progressValue);
}
}
#Override
public void success() {
continueHeadline.setVisibility(android.view.View.VISIBLE);
continueButton.setVisibility(android.view.View.VISIBLE);
}
#Override
public void error(Error.Code code) {}
});
}
}
}
}
Just maybe you dont have permissions to read the file. The file can be stored with permission that doesnt allow you to read in other applications.
Okey so for everyone who can be in the same situation, just re-check if your application didn't loose permission :/ I'am sad
I have to get image from internal storage of mobile device and upload to server. If I select image from sdcard it working fine, while if I select image from internal storage it show error, I am not able to get this error.
Here is logcat output -
07-26 10:32:24.868: W/System.err(24051): java.io.FileNotFoundException: /document/primary:Pictures/Screenshots/Screenshot_20160712-172722.png: open failed: ENOENT (No such file or directory)
I am using this code to get image
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
1);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
OnActivityResult code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// 1
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
try {
// SDK < API11
if (Build.VERSION.SDK_INT < 11)
realPath_1 = RealPathUtil
.getRealPathFromURI_BelowAPI11(getActivity(),
data.getData());
// SDK >= 11 && SDK < 19
else if (Build.VERSION.SDK_INT < 19)
realPath_1 = RealPathUtil.getRealPathFromURI_API11to18(
getActivity(), data.getData());
// SDK > 19 (Android 4.4)
else
realPath_1 = RealPathUtil.getRealPathFromURI_API19(
getActivity(), data.getData());
Log.e("", "Build Version : " + Build.VERSION.SDK_INT);
Log.e("", "Get Data Get Path : " + data.getData().getPath());
if (realPath_1.equalsIgnoreCase("")) {
realPath_1 = data.getData().getPath().toString();
}
Log.e("", "Real Path 1 : " + realPath_1);
file_1 = realPath_1.substring(
realPath_1.lastIndexOf('/') + 1,
realPath_1.length());
Log.i("File Name 1 ", file_1);
txt_file_name_1.setText(file_1);
} catch (Exception e) {
Uri selected = data.getData();
realPath_1 = selected.getPath();
file_1 = realPath_1.substring(
realPath_1.lastIndexOf('/') + 1,
realPath_1.length());
Log.i("File Name 1 ", file_1);
txt_file_name_1.setText(file_1);
}
}
}}
Here is class named - RealPathUtil to get path of images.
My
android:minSdkVersion="14"
android:targetSdkVersion="23"
and called this method from class RealPathUtil
public static String getRealPathFromURI_API19(Context context, Uri uri) {
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
new String[] { id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
Please help and thanks in advance.
Try this:
boolean isKitKat = false;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showFileChooser();
}
});
Code for show file chooser function
private void showFileChooser() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
isKitKat = true;
startActivityForResult(Intent.createChooser(intent, "Select file"), 1);
} else {
isKitKat = false;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file"), 1);
}
}
OnActivity Result code :
#TargetApi(19)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (data != null && data.getData() != null && resultCode == getActivity().RESULT_OK) {
boolean isImageFromGoogleDrive = false;
Uri uri = data.getData();
if (isKitKat && DocumentsContract.isDocumentUri(getActivity(), uri)) {
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
realPath_1 = Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
Pattern DIR_SEPORATOR = Pattern.compile("/");
Set<String> rv = new HashSet<>();
String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
if (TextUtils.isEmpty(rawEmulatedStorageTarget)) {
if (TextUtils.isEmpty(rawExternalStorage)) {
rv.add("/storage/sdcard0");
} else {
rv.add(rawExternalStorage);
}
} else {
String rawUserId;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
rawUserId = "";
} else {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String[] folders = DIR_SEPORATOR.split(path);
String lastFolder = folders[folders.length - 1];
boolean isDigit = false;
try {
Integer.valueOf(lastFolder);
isDigit = true;
} catch (NumberFormatException ignored) {
}
rawUserId = isDigit ? lastFolder : "";
}
if (TextUtils.isEmpty(rawUserId)) {
rv.add(rawEmulatedStorageTarget);
} else {
rv.add(rawEmulatedStorageTarget + File.separator + rawUserId);
}
}
if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
Collections.addAll(rv, rawSecondaryStorages);
}
String[] temp = rv.toArray(new String[rv.size()]);
for (int i = 0; i < temp.length; i++) {
File tempf = new File(temp[i] + "/" + split[1]);
if (tempf.exists()) {
realPath_1 = temp[i] + "/" + split[1];
}
}
}
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
try {
cursor = getActivity().getContentResolver().query(contentUri, projection, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
realPath_1 = cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String docId = DocumentsContract.getDocumentId(uri);
String[] split = docId.split(":");
String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
String selection = "_id=?";
String[] selectionArgs = new String[]{split[1]};
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
try {
cursor = getActivity().getContentResolver().query(contentUri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
realPath_1 = cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("com.google.android.apps.docs.storage".equals(uri.getAuthority())) {
isImageFromGoogleDrive = true;
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
Cursor cursor = null;
String column = "_data";
String[] projection = {column};
try {
cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(column);
realPath_1 = cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
realPath_1 = uri.getPath();
}
try {
Log.d(TAG, "Real Path 1 : " + realPath_1);
file_1 = realPath_1.substring(realPath_1.lastIndexOf('/') + 1, realPath_1.length());
Log.i("File Name 1 ", file_1);
} catch (Exception e) {
e.printStackTrace();
}
}
}}
Here file_1 = get image name and realPath_1 show full path of image from internal or external storage.
Happy to help :)
Use bellow code for get file URL.
public static String getPath(final Context context, final Uri uri) {
// check here to KITKAT or new version
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection,
selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}
Your code looks fine you just have to get runtime permissions from user. In Andorid 6.0 or later needs permission from user at runtime only declaring them manifest is not enough.
See here my earlier post about how to get runtime permissions.
Update:
private void showFileChooser() {
try {
// Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivityForResult(i, RESULT_LOAD_IMAGE);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, RESULT_LOAD_IMAGE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
//Uri selectedImage = data.getData();
txt_file_name_1.setText(data.getData().toString());
}
}