I am writing an app which calculates the horizontal DPI of an image the user inputs. I am using the Sanselan library in Java's Common Imaging library. However, when I use the File
object that is constructed from the path that is retrieved from a URI
in an onActivityResult
listener, passing it in as the parameter to a function that computes the horizontal DPI returns a FileNotFoundException
:
onActivityResult error: java.io.FileNotFoundException: /document/image:53066 (No such file or directory)
Below is my code:
build.gradle (App):
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.changingimageviewwidth"
minSdkVersion 26
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "org.apache.sanselan:sanselan:0.97-incubator"
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="analyzePhoto"/>
</LinearLayout>
MainActivity.java:
package com.example.changingimageviewwidth;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import org.apache.sanselan.ImageInfo;
import org.apache.sanselan.Sanselan;
import java.io.File;
public class MainActivity extends AppCompatActivity {
// Request code used when reading in a file
private Integer READ_IN_FILE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void analyzePhoto(View v) {
// Specify that only photos are allowed as inputs (.jpg, .png).
Intent photoInputSpecs = new Intent(Intent.ACTION_GET_CONTENT);
photoInputSpecs.setType("image/*");
Intent photoInputHandler = Intent.createChooser(photoInputSpecs, "Choose a file");
startActivityForResult(photoInputHandler, 0);
}
// Processes the results of Activities.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// Processes the photos that the user selected
if (requestCode == READ_IN_FILE) {
System.out.println("Let us process the photos that the user selected.");
// Retrieve the photo's location
Uri photoUri = data.getData();
String photoPath = photoUri.getPath();
File photoFile = new File(photoPath);
ImageInfo image1Info = Sanselan.getImageInfo(photoFile);
int image1HorizontalDPI = image1Info.getPhysicalWidthDpi();
int image1VerticalDPI = image1Info.getPhysicalHeightDpi();
System.out.println("[image1HorizontalDPI] = " + image1HorizontalDPI);
System.out.println("[image1VerticalDPI] = " + image1VerticalDPI);
}
}
catch (Exception ex) {
System.out.println("onActivityResult error: " + ex.toString());
}
}
}