Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android从文件路径获取图像到位图_Java_Android_Android Studio_Bitmapimage - Fatal编程技术网

Java Android从文件路径获取图像到位图

Java Android从文件路径获取图像到位图,java,android,android-studio,bitmapimage,Java,Android,Android Studio,Bitmapimage,我正在尝试从手机上的特定文件路径设置图像。文件路径指向手机上的照片。文件路径可能如下所示 /存储/模拟/0/Pictures/picture.jpg 这是代码 Bitmap image = null; //trying to set the image from filepath try { image = BitmapFactory.decodeStream((InputStream) new URL(filepath).getContent()); } catch (Malform

我正在尝试从手机上的特定文件路径设置图像。文件路径指向手机上的照片。文件路径可能如下所示

/存储/模拟/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();
}

最后,图像未设置。

使用此方法从文件路径获取位图

public Bitmap getBitmap(String path) {
Bitmap bitmap=null;
try {
    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 bitmap ;
}
试试这个

     @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  Intent intent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  startActivityForResult(intent, 0);
 }});
 }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK){
 Uri targetUri = data.getData();
 textTargetUri.setText(targetUri.toString());
 Bitmap bitmap;
 try {
  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
  targetImage.setImageBitmap(bitmap);
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
试试这个:

File sd = Environment.getExternalStorageDirectory();
File imageFile = new File(sd+filepath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap image = BitmapFactory.decodeFile(imageFile.getAbsolutePath(),bmOptions);

URL
还是
URI
?最好使用
AsyncTask
从URL下载图像。图像在电话上,而不是在互联网上。您可以发布
imgurl
?它实际上是一个文件路径。不是吗?使用
Bitmap Bitmap=BitmapFactory.decodeFile(filePath)
图像在手机上,而不是在互联网上