1

I'm trying to make a picture selected by the user to go to my web server through POST request. So I need to encode my pic in Base64 but I have a problem, it's that logcat returns a FileNotFoundException that I dont understand :

Unable to decode stream: java.io.FileNotFoundException

Here is my onActivityResult :

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

        final Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Log.d("Toast", filePathColumn[1]);
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        imgViewScan.setImageURI(selectedImage);
        try
        {
            InputStream stream = getContentResolver().openInputStream(
                    data.getData());
            Bitmap bm = BitmapFactory.decodeStream(stream);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            //Bitmap bm = BitmapFactory.decodeFile(selectedImage.toString(),options);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
            byte[] b = baos.toByteArray();
            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
            new EnvoiPhotoTask().execute(encodedImage);
        } catch(IOException e)
        {
            Log.e("ScanAc", e.toString());
        }


    }
}

and here is my manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.partenaires.legimetrie.legimetrieapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<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/Theme.AppCompat.Light.NoActionBar">
    <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=".InscriptionActivity" />
    <activity android:name=".ConnexionActivity" />
    <activity android:name=".ServicesActivity" />
    <activity android:name=".InterventionRapideActivity" />
    <activity android:name=".InterventionDetailleeActivity" />
    <activity android:name=".ScanActivity" />
    <activity android:name=".ContactActivity"></activity>
</application>

And I have a function `

    public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

i'm desperate since 4 days ago...

Orionss
  • 725
  • 2
  • 10
  • 26

1 Answers1

1

I found what went wrong (obviously I have an other problem then but x)) So the problem that I used

InputStream stream = getContentResolver().openInputStream(
                data.getData());

but I already did :

final Uri selectedImage = data.getData();

but you can't get the datas two times so I just did :

InputStream stream = getContentResolver().openInputStream(
                selectedImage);

and it went good !

Orionss
  • 725
  • 2
  • 10
  • 26