I select file using startActivityForResult, which open deafult File Manager, and it should return path of my selected file. After I'm trying to copy that file in my internal storage and I'm trying to open that file using another app, i.e. opening a .jpg with Gallery, .pdf with Acrobat... But the code don't work
This is my code:
package com.example.francesco.pdf_viewer;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Calendar;
import java.net.URI;
import static android.util.JsonToken.NULL;
import static java.net.Proxy.Type.HTTP;
public class MainActivity extends AppCompatActivity {
static final int SELECT_FILE_REQUEST = 2;
private Uri uri;
Button clickButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
clickButton = (Button) findViewById(R.id.clickPath);
clickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File f = new File(uri.getPath());
Uri u = Uri.fromFile(f);
Intent filePathIntent = new Intent(Intent.ACTION_VIEW, u);
startActivity(filePathIntent);
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectFile();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_FILE_REQUEST) {
if (resultCode == RESULT_OK) {
Uri fileUri = data.getData();
//String filename = fileUri.getPath();
uri = fileUri;
Toast.makeText(getApplicationContext(), fileUri.toString(), Toast.LENGTH_LONG).show();
}
}
}
private void selectFile() {
Intent selectFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
selectFileIntent.setType("*/*");
selectFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(selectFileIntent, "Select a File"), SELECT_FILE_REQUEST);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(this, "Please install a File Manager", Toast.LENGTH_SHORT).show();
}
}
}
Thank you, Sorry for English
EDIT: The uri, that i get from startActivityForResult, is this : "content:://com.android.providers.downloads.documents/document/10"
But the original path of the file, that i download, is this: /storage/emulated/0/Download/1/android-n-n.jpg