gpt4 book ai didi

java.io.FileNotFoundException 即使 imageView 可以显示图片

转载 作者:行者123 更新时间:2023-11-30 01:03:03 29 4
gpt4 key购买 nike

我试图让用户选择的图片通过 POST 请求转到我的网络服务器。所以我需要在 Base64 中对我的图片进行编码,但我遇到了一个问题,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());
}


}
}

这是我的 list :

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

我从 4 天前就开始绝望了......

最佳答案

我发现出了什么问题(显然我还有其他问题,但是 x))所以我用的问题

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

但我已经这样做了:

final Uri selectedImage = data.getData();

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

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

一切顺利!

关于java.io.FileNotFoundException 即使 imageView 可以显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206203/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com