filenotfoundexception,即使imageView可以显示图片



我试图让用户选择的图片通过POST请求到我的web服务器。所以我需要在Base64编码我的pic,但我有一个问题,这是logcat返回一个FileNotFoundException,我不明白:

Unable to decode stream: java.io.FileNotFoundException
这是我的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());
        }

    }
}

这是我的清单:

<?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>

我有一个函数

    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
        );
    }
}

我发现哪里出了问题(显然我有其他问题,但x))所以我用

的问题
InputStream stream = getContentResolver().openInputStream(
                data.getData());

但是我已经做了:

final Uri selectedImage = data.getData();

但是你不能获得两次数据,所以我就这样做了:

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

相关内容

  • 没有找到相关文章

最新更新