Getting full file path of android file? - android

Good day, all! I'm currently coding a plugin for Unity so that the user can access the file browser and pick an app. Upon picking the app, the file path is supposed to be saved. My code works so far, it's able to get the file path of the file but not the full one.
This is my code so far
int PICKFILE_RESULT_CODE = 1;
#Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/vnd.android.package-archive");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
}
#SuppressLint("InlinedApi")
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode==RESULT_OK && requestCode==PICKFILE_RESULT_CODE && data != null)
{
String apkPath = data.getData().getPath();
UnityPlayer.UnitySendMessage("Browser", "OnApkPick", apkPath);
Browser.this.finish();
super.onActivityResult(requestCode, resultCode, data);
}
}
Thanks in advance.

Related

Open a default folder path in Android?

Issue:
Office is a folder in internal memory of Android.
Clicking a button in screen should always take one to default folder, Office.
Appreciate help as no accepted answers found.
In onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null)
return;
if (requestCode == viewfilerequestcode){
Uri Fileuri = data.getData();
String DFLocation="/mnt/sdcard/Office/";
String FilePath="";
if (FilePath.trim().equals(DFLocation.concat(GettheFilename(Fileuri))))
{
FilePath = DFLocation.concat(GettheFilename(Fileuri));
}
......
.......
........
}
.........
..........
}
Intent start:
btnview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent fileintent;
String[] mimestoview =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx };
fileintent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
fileintent.setType("*/*");
fileintent.putExtra(Intent.EXTRA_MIME_TYPES, mimestoview );
fileintent.addCategory(Intent.CATEGORY_OPENABLE);
fileintent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
fileintent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivityForResult(fileintent, viewfilerequestcode);
} catch (ActivityNotFoundException e) {
lbl.setText("No activity to handle picking a file. Showing alternatives.");
}
}
});

How to add Internal and External storage to File Chooser in android

My sample app is open file chooser and select file / directory then getting the path into EditText, I tried the methods in this question and I reached to this result
public class MainActivity extends AppCompatActivity {
private Button browse;
private EditText editTextPath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browse = (Button) findViewById(R.id.browse);
editTextPath = (EditText) findViewById(R.id.path);
browse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent,"Select file or dir"), 1);
setResult(Activity.RESULT_OK);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
String Fpath = data.getDataString();
editTextPath.setText(Fpath);
}
}
}
I want to add internal and external storage like this to file chooser
The user can tap the "..." affordance in the action bar and choose "Show internal storage" to display those options.
There is no official support for anything on ACTION_GET_CONTENT or ACTION_OPEN_DOCUMENT to show internal storage options automatically, though hopefully this will be supported someday.
you must use Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
insted Intent intent = new Intent(Intent.ACTION_GET_CONTENT );

Android - Choose File and Get Absolute Path

I am developing an app which enables the user to select a file and do processing on it after getting the path of it, I have written a code which gets me the path like this
private void OpenFile()
{
Intent i = new Intent(Intent.ActionGetContent);
i.SetType("application/zip");
StartActivityForResult(i,0);
}
In activity for result I am extracting the path as follows:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 0)
{
if (resultCode == Result.Ok)
{
string uri = data.DataString;
System.Uri myUri = new System.Uri(uri, System.UriKind.Absolute);
Android.Net.Uri uris = Android.Net.Uri.FromParts(data.Scheme, myUri.LocalPath, myUri.Fragment);
// string a= myUri.LocalPath;
System.IO.Stream input= ContentResolver.OpenInputStream(uris);
string uri = data.DataString;
ZipLogic.Unzip(uri);
}
}
}
And the results are in such pattern:
content://com.android.externalstorage.documents/document/xxxx-83BB%3xxx%2Fxxx.zip
But this path when I try to access from returns DirectoryNotFound Exception
I am unable to resolve how to open this path as a Stream.
Luckily, I found the answer by watching closely the Intent data.
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
base.OnActivityResult(requestCode, resultCode, intent);
if (requestCode == 0)
{
if (resultCode == Result.Ok)
{
string uri = data.DataString;
//intent variable has a Field named Data which is the complete URI for the file.
Android.Net.Uri uris = Android.Net.Uri.FromParts(intent.Data.Scheme, intent.Data.SchemeSpecificPart, intent.Data.Fragment);
System.IO.Stream input = ContentResolver.OpenInputStream(intent.Data);
//related tasks
}
}
}
Explanation:
The selected file result has a field named Data in the Intent object, that is basically the Uri Invoker which tends to be a URI object
I used it to get an input stream from ContentResolver.

How can I get the URI with a custom camera in Android?

I'm switching from using the default camera to finally growing a pair and deciding to make a custom camera. It's only showing me how much I don't fully understand.
Here is basically way I have been doing things in the main activity as far as photos go, but it will no longer work:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_PHOTO_REQUEST) {
if (data == null) {
Toast.makeText(this, "General Error!", Toast.LENGTH_LONG).show();
}
else {
mMediaUri = data.getData();
}
Log.i(TAG, "Media URI: " + mMediaUri);
}
else {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
}
(...)
Here is the picture saving function along with a couple others of the new camera:
(...)
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Picture taken");
String path = savePictureToFileSystem(data);
setResult(path);
finish();
}
private static String savePictureToFileSystem(byte[] data) {
File file = getOutputMediaFile();
saveToFile(data, file);
return file.getAbsolutePath();
}
private void setResult(String path) {
Intent intent = new Intent();
intent.putExtra(EXTRA_IMAGE_PATH, path);
setResult(RESULT_OK, intent);
}
*Credit to Paul Blundell
(...)
What do I need to do so that the main activity can receive the image's URI in the onactivityresult instead of the path String? Are URIs even applicable when it comes to custom cameras?
Please and thanks.
You use custom camera, but want to do it via Intent? OK, you have the absolute file path in
String abspath = data.getExtras().getString(EXTRA_IMAGE_PATH);
mMediaUri = Uri.fromFile(new File(abspath));

camera activity does not return to the previous/caller activity

I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}

Categories

Resources