Edv*_*erg 6 java android bitmapimage android-studio
我正在尝试从手机上的特定文件路径设置图像.文件路径是手机上的照片.文件路径看起来像这样
/storage/emulated/0/Pictures/picture.jpg
这是代码.
Bitmap image = null;
//trying to set the image from filepath
try {
image = BitmapFactory.decodeStream((InputStream) new URL(filepath).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (image == null) {
//This becomes true because the image is not set
Toast.makeText(getApplicationContext(),"image == null",Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
最后,图像未设置.
Amn*_*niX 15
使用此方法从Filepath获取位图
public Bitmap getBitmap(String path) {
try {
Bitmap bitmap=null;
File f= new File(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
return null;
}}
Run Code Online (Sandbox Code Playgroud)
尝试这个:
File sd = Environment.getExternalStorageDirectory();
File imageFile = new File(sd+filepath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap image = BitmapFactory.decodeFile(imageFile.getAbsolutePath(),bmOptions);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14642 次 |
最近记录: |