I'm failing on this task in both cases:
1. Trying to open it from the phone memory
2. Trying to open it from the assets
I have render() method which is in 1st case looking like this:
private void render() {
try {
imageView = (ImageView) findViewById(R.id.page);
int REQ_WIDTH = 1;
int REQ_HEIGHT = 1;
REQ_WIDTH = imageView.getWidth();
REQ_HEIGHT = imageView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_HEIGHT, Bitmap.Config.ARGB_4444);
File file = new File(Environment.getExternalStorageDirectory() + "/Download/test.pdf");
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
if (currentPage < 0) {
currentPage = 0;
} else if (currentPage > renderer.getPageCount()) {
currentPage = renderer.getPageCount() - 1;
}
Matrix matrix = imageView.getImageMatrix();
Rect rect = new Rect(0, 0, REQ_WIDTH, REQ_HEIGHT);
renderer.openPage(currentPage).render(bitmap, rect, matrix, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
imageView.setImageMatrix(matrix);
imageView.setImageBitmap(bitmap);
imageView.invalidate();
renderer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Most interesting part of thrown exception is here. In case you want to see whole logs click here.
And yes, I've added needed permission in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.john.pdfreader">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BookActivity">
<intent-filter>
<action android:name="com.example.schoolreader.BookActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Let's take a look on the 2nd case(reading from the assets):
// ...previous code
ParcelFileDescriptor input = getAssets().openFd("test.pdf").getParcelFileDescriptor();
PdfRenderer renderer = new PdfRenderer(input);
// ..previous code
In this case it says the same, but if I remember correctly there was smth saying cannot create document, but now it's gone and exception looks the same. Anyway, why did I start talking about 2nd case? There's an open issue saying that META-INF should be at the end of the apk. Mine is the at the end and it still not working.
Frankly, I've tried even an example from Androidstudio itself. And you know what? It's working! But only with their sample.pdf and when I try to replace it with mine it just crashes. Do you have any idea what I can try to make pdf rendering on my Android?
Everything was tested on Nexus 5, Android 6.0 Marshmallow.
In addition to the permission in the manifest, you may need to ask for permission explicitly. Something like:
int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED)
{
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}
Related
I am working on a photo editor app in which after editing my picture I save it into my local storage. It is working fine till android 9 but not on android 10. It shows exception of "No such file or directory found" in Android 10. After some research I found that getExternalFilesDir() is deprecated in android Q+. But I cannot find any proper way to do it in android 10. So please if anyone can provide a tutorial it would be really helpful.
I've added and granted uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in case of it was the issue, and it didn't solve anything.
This is my try (Used ParcelFileDescriptor):
private void fileAccessForAndroidQ(Uri fileUri){
try {
ParcelFileDescriptor parcelFileDescriptor = this.getContentResolver().openFileDescriptor(fileUri, "r", null);
InputStream inputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
Cursor returnCursor =
getContentResolver().query(fileUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex);
file = new File(this.getFilesDir(), fileName);
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copyStream(inputStream, outputStream);
}catch (Exception e){
Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Any kind of help would be appreciated.
If you target Android 10 (API level 29) or higher, set the value of requestLegacyExternalStorage to true in your app's manifest file:
Documentation
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appname"
android:installLocation="auto">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the best I could find:
https://developer.android.com/training/data-storage/app-specific#external
Basically, you now use app-specific directories for your files. For example:
#Nullable
File getAppSpecificAlbumStorageDir(Context context, String albumName) {
// Get the pictures directory that's inside the app-specific directory on
// external storage.
File file = new File(context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES), albumName);
if (file == null || !file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
I found this working for me. i am trying to list all file on ListView
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
} else {
File s = Environment.getExternalStorageDirectory();
Log.d("Path ",Arrays.toString(s.listFiles()));
File[] s1 = new File[s.listFiles().length];
for(int i=0;i<s.listFiles().length;i++){
s1[i]= new File(s.listFiles()[i].toString().replace("/storage/emulated/0/", ""));
}
ArrayAdapter<File> adapter = new ArrayAdapter<File>(this,R.layout.support_simple_spinner_dropdown_item,s1);
l1.setAdapter(adapter);
}
I need help with permission to save file in Android emulator ... I've had added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="true" />
in AndroidManifest.xml
My code for save file:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Img");
myDir.mkdirs();
long n = System.currentTimeMillis();
String fname = "IMG_" + n + ".jpeg";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out2 = new FileOutputStream(file);// here fire exception
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out2);
out2.flush();
out2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.denis.calculator">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InfoActivity"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" android:required="true" />
Any ideas pls?
Solution is downgrade from Nougat to KitKat! thanks for advices
Replace the below lines
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Img");
With
File myDir = new File(Environment.getExternalStorageDirectory(),"Img");
Also remove the below lines from your code
if (file.exists())
file.delete();
Because the way you defining the file name will never gonna have the same file name. So validating the existence of file is irrelevant here.
You also have to ask writing permission with the user if your app is going to be used for Android 6.0 or above. Refer the link given below for details:
https://developer.android.com/training/permissions/requesting.html
You can use the solution defined in the library I've recently created this repository including a demo for Permission.
https://github.com/eeshan-jamal/DroidLibX
I will later make it available through Maven but for now you have to import the droidlibx library module in your project.
I am unable to create a directory in the external storage folder. I have created a new project, modified the manifest and added the code as seen below. Any help would be greatly appreciated. I updated my post to include that the code provided below does not work. The only thing I can think is the emulator is the problem. I am running Nexis API 32 x64.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.us.foobar"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Source:
public void mkdir() throws Exception
{
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
String dir = root + "/Foobar";
File file = new File(dir);
if (!file.exists())
{
if (!file.mkdirs())
{
throw new Exception("Unable to create application data directory.\r\n\r\nDirectory: " + dir);
}
}
}
I call the same function on onCreate and works fine. Look at my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test();
}
public void test() {
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
String dir = root + "/Foobar";
File file = new File(dir);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("Error", "Unable to create application data directory.\r\n\r\nDirectory: " + dir);
}
}
}
}
Using API 23 I had to request the permissions prior to writing to the file system as follows:
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_EXTERNAL_STORAGE
);
I'm trying to get the size of a remote video using this class and i'm getting IllegalArgumentException if the video is remote.
the video is an mp4 stored in one server...
the video plays correctly if i play it with mediaplayer, but it gives the error if i try to do this:
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap bmp = null;
retriever.setDataSource(context, uri);
bmp = retriever.getFrameAtTime();
videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
e.printStackTrace();
}
the error is thrown in this line:
retriever.setDataSource(context, uri);
and uri contains Uri.parse("http://www.myweb.com/myvideo.mp4");
what is wrong in the code?
12-19 13:38:08.610: W/System.err(13333): java.lang.IllegalArgumentException
12-19 13:38:08.611: W/System.err(13333): at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:175)
Maybe you are running into this bug. If so try:
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap bmp = null;
retriever.setDataSource("http://www.myweb.com/myvideo.mp4", new HashMap<String, String>());
bmp = retriever.getFrameAtTime();
videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
e.printStackTrace();
}
If that doesn't work you can always try FFmpegMediaMetadataRetriever:
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
try {
Bitmap bmp = null;
retriever.setDataSource("http://www.myweb.com/myvideo.mp4"));
bmp = retriever.getFrameAtTime();
videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
e.printStackTrace();
}
retriever.release();
I was getting the same error, I am using android 10.
I solved just putting android:requestLegacyExternalStorage="true" in Manifest inside application.
See here
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
In my case, I was creating a simple metadata extraction test app, so I copied a file to my phone using adb, like so:
adb push 350950598.mp4 /sdcard/Movies
but I forgot to add the read external storage directory permission in the app manifest.
Specifically:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.cool.package.name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>
Adding those permissions fixed it for me, even for the simple file string call:
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(movie.getPath());
And of course, if you're targeting API 23+ marshmallow then you'll have to dynamically ask for those permissions, as well.
try {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap bmp = null;
retriever.setDataSource(uri.toString(), new HashMap<String, String>());
bmp = retriever.getFrameAtTime();
videoHeight = (int) (bmp.getHeight()*((float)getIntWidth()/bmp.getWidth()));
} catch (Exception e) {
e.printStackTrace();
}
You need to give runtime permissions if you are using Android Marshmallow or later.
Android Manifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.cool.package.name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
Then add code for runtime permissions in your activity. After that, run your application and it should work.
if you are uses android 10 or above one then you need to mention requestLegacyExternalStorage to true.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true"
android:theme="#style/AppTheme">
Note : if by doing this still you're facing the same issue then you need to reinstall the app. :)
I have problem with finding photo after saving.
Here is how i am creating Intent to take photo from camera and save filePath to it.
private void takePhoto(int position)
{
Meter meter = adapter.getItem(position);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File tempFile = createTempFile(meter.id, JPEG_FILE_SUFFIX, storageDir);
if(tempFile != null)
{
lastPhotoPath = tempFile.getAbsolutePath();
Log.d(TAG, "temp picture path=" + lastPhotoPath);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(tempFile));
try
{
startActivityForResult(takePictureIntent, TAKE_PHOTO_REQUEST);
lastPhotoPosition = position;
}
catch(ActivityNotFoundException exc)
{
Log.e(TAG, "activity to take photo not found");
}
}
}
Then later i want to upload this image to server.
Here is how i am doing this.
public void compose(OutputStream out) throws DataStorageException
{
Log.d("MainMenuActivity", "file not found in path " + path);
InputStream in = null;
try
{
in = new BufferedInputStream(new FileInputStream(path));
// TODO: there is a better way
byte[] buf = new byte[8192];
while(true)
{
int length = in.read(buf);
if(length < 0)
break;
out.write(buf, 0, length);
}
}
catch(FileNotFoundException exc)
{
throw new DataStorageInternalErrorException("FileNotFoundException, FileRequestComposer compose");
}
catch(IOException exc)
{
// TODO: probably network error
throw new DataStorageInternalErrorException("IOException, FileRequestComposer compose");
}
finally
{
if(in != null)
{
try
{
in.close();
}
catch(IOException exc)
{
// FIXME
}
}
}
}
I check filePath from saving and filePath to save they both completely identical:
temp picture path=/mnt/sdcard/Pictures/19520983731349.jpg
file not found in path /mnt/sdcard/Pictures/19520983731349.jpg
And the exception is thrown in method compose()
FileNotFoundException, FileRequestComposer compose
Any ideas where i am doing wrong?
P.S. And i can't see this file in /mnt/sdcard/Pictures mb. he is lost or somethinf like this? Please suggest any ideas.
P.P.S Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vodomer"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="19"/>
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:label="#string/app_name"
android:name="com.vodomer.DatabaseApplication" >
<activity android:name="Vodomer"
android:label="#string/app_name"
android:icon="#drawable/icon"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.AddressesListActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".activity.MainMenuActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".activity.MetersActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".activity.PersonalAccountActivity"
android:theme="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
>
</activity>
</application>
</manifest>
The problem is likely with the way you create File. Instead of createTempFile try regular constructor :
File tempFile = new File(storageDir, meter.id + JPEG_FILE_SUFFIX);
Generally, you also want to make sure that your storageDir folder exists and create it with mkdirs if needed.
Add this path for storing image on sdcard.
final String uploadFilePath = "/mnt/sdcard/";