gpt4 book ai didi

android - 如何在从内部存储中选择的 imageView 上显示图像?

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:10 24 4
gpt4 key购买 nike

我不熟悉在 android 中处理图像。我想从内部存储加载图像,但它给了我权限被拒绝的错误,然后我将权限添加到 android list 文件。但是我仍然无法完成我的任务。

这是我的代码:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView)findViewById(R.id.imageView);
Bitmap bm = BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg");
image.setImageBitmap(bm);

}
}

日志:

01-16 15:16:11.345 6533-6533/com.example.jaytanna.imagemap E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg: open failed: EACCES (Permission denied)

它没有显示任何图像。请帮我解决这个问题。谢谢。

最佳答案

检查这个

     File imgFile = new  File("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg");
if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

};

并将此权限包含在 list 文件中:

<manifest>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
<application>
...
<activity>
...
</activity>
</application>
</manifest>

对于 API 23+,您需要请求读/写权限,即使它们已经在您的 list 中。

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
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
);
}
}

并处理响应,一些例子:

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! Do the
// contacts-related task you need to do.

} else {

// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}

// other 'case' lines to check for other
// permissions this app might request
}
}

有关请求 API 23+ 权限的官方文档,请查看 https://developer.android.com/training/permissions/requesting.html

关于android - 如何在从内部存储中选择的 imageView 上显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673282/

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