How to compare Uri and file android - android

I am launching a file picker intent. And I am getting a URI of the file from data.getData() method on onActivityResult . There I have a utility class for getting the real path of files from the URI. I want to check whether the path returned from the utility class is valid or not by comparing it with URI. Let assume the utility class returned the path of a different file by mistake. I don't want to use the file name to compare. is there any way?
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==IMAGE_FILE_PICKER_CODE&&resultCode==RESULT_OK){
String path = null;
try {
path = new PathUtils(getActivity()).getPath(data.getData()); //Utility class to get path from uri
File file = new File(path); //want to compare this file with data.getData()

Related

Convert File to DocumentFile

I want to convert File (/storage/A54E-14E9/bp.mp4) to DocumentFile
Uri of DocumentFile should be content://com.android.externalstorage.documents/tree/A54E-14E9%3A/document/A54E-14E9%3Abp.mp4
but when I use DocumentFile.fromFile(file).getUri() it returns not correct Uri:
file:///storage/A54E-14E9/bp.mp4
So it's different than when you get it in onActivityResult after calling startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), REQUEST_CODE_STORAGE_ACCESS);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_STORAGE_ACCESS && resultCode == RESULT_OK) {
Uri treeUri = data.getData();
mDocumentDir = DocumentFile.fromTreeUri(this, treeUri);
for (DocumentFile file : mDocumentDir.listFiles()) {
// one of them is content://com.android.externalstorage.documents/tree/A54E-14E9%3A/document/A54E-14E9%3Abp.mp4
Log.i(TAG, file.getUri());
}
}
}
or if it's not possible then how to find what equals to File (/storage/A54E-14E9/bp.mp4) in:
// in onActivityResult
for (DocumentFile file : mDocumentDir.listFiles()) {
Log.i(TAG, file.getUri());
}
it's not good idea to just check file names, I need to check absolute paths (if they equals), also file can be placed in subfodler, so it makes things more difficult
My task is to delete File from MicroSD card, it can be done using DocumentFile after requesting storage (ACTION_OPEN_DOCUMENT_TREE) but I need somehow to get the right DocumentFile which equals my File

How to convert content:// to file after image capture android?

I am capturing image using camera in android using the following code snippet.
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(Constants.ATTACH_IMAGE);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
imageIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());startActivityForResult(imageIntent, Constants.ATTACH_IMAGE);
in onActivityResult, I want to convert the "content://" uri back to File, compress it and show a thumb nail.
How can I do it?
Edit :-
What I have tried:-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("activity result","coming" + String.valueOf(requestCode)+ contentFileUri);
// View popupView = getActivity().getLayoutInflater().inflate(R.layout.popup_add_snag, null);
// LinearLayout attachLayout = (LinearLayout)popupView.findViewById(R.id.attachLayout);
// ImageView photoImageicon = (ImageView)popupView.findViewById(R.id.imageicon);
try{
// if (resultCode == Constants.RESULT_OK){
if(contentFileUri !=null) {
Log.d("fileuri", contentFileUri.toString());
String attachmentType = "IMAGE";
// mAdapter.attachmentType=attachmentType;
photoImageIcon.setVisibility(View.VISIBLE);
// attachLayout.setVisibility(View.INVISIBLE);
Toast.makeText(getActivity().getApplicationContext(), R.string.successfull_image, Toast.LENGTH_SHORT).show();
Log.d("fileuri", contentFileUri.toString());
InputStream inputStream = getContext().getContentResolver().openInputStream(contentFileUri);
contentFileUri looks like this:- "content://com.fa.ha.provider/external_files/F/7c7.jpeg"
But above results in "FileNotFound Exception, not a directory" at the last line.
Ideally, just hang onto the File that getOutputMediaFileUri() is creating. Hopefully, getOutputMediaFileUri() is setting up a FileProvider Uri for you, based off of a File, so if you hold onto the File, you do not need to worry about "converting" anything. See this sample app.
If getOutputMediaFileUri() is poorly written, perhaps you do not have a File. In that case, in onActivityResult(), you can pass the Uri to an image-loading library (Picasso, Glide, etc.) and have it populate an ImageView or simply load the Bitmap for you.

Android How do I get text type file's name and path from local device?

search = (Button)findViewById(R.id.FindButton);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,1);
}
});
I used ACTION_GET_CONTENT function to get file's name and local path.(setType is / but I want to get json file)
so this is my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 1){
}
}
When I choose data and then what code do I put in there?
I used ACTION_GET_CONTENT function to get file's name and local path
ACTION_GET_CONTENT has nothing to do with files.
When I choose data and then what code do I put in there?
You are welcome to use a ContentResolver to query() for the OpenableColumns, which can give you a "display name". That is not necessarily a filename, because you are working with content, not files. See this sample app for getting the OpenableColumns using a CursorLoader.
However, you cannot get a "file path", as this is not a file. It is content. What you are asking is akin to asking how to get the file path for https://stackoverflow.com/questions/43878541/android-how-do-i-get-text-type-files-name-and-path-from-local-device.
You can use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Decent JSON parsers (e.g., Gson) accept an InputStream and can parse the JSON from there.

filedescriptor use to render pdf file from external storage in android

I have downloaded a pdf file in external storage directory and i want to reder it using pdfrender in android.
i have successfully rendered the pdf saved in my asset folder but now i need to render the file that is saved in external directory. here is the code i am using
String filePath1 = Environment.getExternalStorageDirectory().toString()+"/Mock-up Presentation.pdf";
//filePath1 is the location for file i want to render
File file = new File(filePath1);
mFileDescriptor=getActivity().getAssets().openFd("sample.pdf").getParcelFileDescriptor();//sample pdf is saved in asset folder in project which i have rendered //already
// This is the PdfRenderer we use to render the PDF.
mPdfRenderer = new PdfRenderer(mFileDescriptor);
Any help would be appreciated.
If you faced this problem in project that is targeting android 9+,
Get Complete Code For Solution.from Here
Then you can follow some Steps:
Step 1: Open an Intent for selecting PDF file.
/This Code will help to open Intent../
public final int PDF_REQUEST_CODE =1200;
Intent intent=new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
startActivityForResult(intent,PDF_REQUEST_CODE);
Step 2: now catch result returned by above intent.
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==PDF_REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
Uri uri=data.getData();
PdfRendererBasicViewModel pdfRendererBasicViewModel =new
ViewModelProvider(this).get(PdfRendererBasicViewModel.class);
pdfRendererBasicViewModel.setUri(uri);
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PdfRendererBasicFragment())
.commitNow();
}
}
Step 3: Need some Modifications on pdfRendererBasicViewModel class.
First of all need to declare methods that set/get file's Uri in viewmodel.
as we called setUri(). in onActivityResult() method.
After That update replace openPdfRenderer() method with
private static Uri uri; `
private void openPdfRenderer() throws IOException {
if(getUri()!=null){
mFileDescriptor =
getApplication().getContentResolver().openFileDescriptor(getUri(), "r");
}
if (mFileDescriptor != null) {
mPdfRenderer = new PdfRenderer(mFileDescriptor);
}
}`
now try to run ...
If You want a complete Solution then you can import from github here

localized file names display incorrectly after uploading with openFileChooser() in android webView

Android webView can upload files with a file picker by using the hidden method openFileChooser(). This can work well with file with English names. But when uploading files with localized names, like "你好.txt", the file name displays incorrectly in a un-decoded Uri style("%E4%BD%A0%E5%BD.txt"). As I can only pass a Uri, there's no way I can find to pass a Uri decoded file name. How can I resolve this issue?
Code example:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
Uri result = intent.getData();
mUpload.onReceiveValue(result);
mUpload = null;
}
}

Categories

Resources