Java Android BinaryFactory.decodeStream始终返回null
我正在尝试下载图像并使用BitmapFactory将其解码为位图,但decodeStream始终返回null。我在谷歌上搜索了许多类似的问题,尝试了许多例子,但没有找到解决办法 这是我的密码:Java Android BinaryFactory.decodeStream始终返回null,java,android,android-studio,bitmapfactory,Java,Android,Android Studio,Bitmapfactory,我正在尝试下载图像并使用BitmapFactory将其解码为位图,但decodeStream始终返回null。我在谷歌上搜索了许多类似的问题,尝试了许多例子,但没有找到解决办法 这是我的密码: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void downloadButton(View v)
{
String imageUrl = "http://www.picgifs.com/bird-graphics/bird-graphics/elf-owl/bird-graphics-elf-owl-527150.bmp";
new Thread(new ImageDownloader(imageUrl)).start();
}
public void showImageButton(View v)
{
Bitmap image = ImageHandler.getImage();
if (image == null)
Log.e("Error", "No image available");
else {
ImageView imageView = (ImageView)findViewById(R.id.ImageView);
imageView.setImageBitmap(image);
}
}
}
class ImageHandler
{
static protected List<Bitmap> imagesList;
static public void addImage(Bitmap image)
{
imagesList.add(image);
}
static public Bitmap getImage()
{
if (!imagesList.isEmpty())
return imagesList.get(0);
else
return null;
}
}
class ImageDownloader implements Runnable
{
public Bitmap bmpImage;
private String urlString;
public ImageDownloader(String urlString)
{
this.urlString = urlString;
}
public void run()
{
try
{
AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
HttpGet getRequest = new HttpGet(urlString);
HttpResponse response = client.execute(getRequest);
HttpEntity entity = response.getEntity();
InputStream inputStream = null;
inputStream = (new BufferedHttpEntity(entity)).getContent();
bmpImage = BitmapFactory.decodeStream(inputStream);
//bmpImage = BitmapFactory.decodeStream(new URL(urlString).openConnection().getInputStream());
}
catch (Exception e)
{
Log.e("ImageDownloadError", e.toString());
return;
}
if (bmpImage != null)
{
ImageHandler.addImage(bmpImage);
Log.i("Info", "Image download successfully");
}
else
Log.e("Error", "Bitmap is null");
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
公共作废下载按钮(视图v)
{
字符串imageUrl=”http://www.picgifs.com/bird-graphics/bird-graphics/elf-owl/bird-graphics-elf-owl-527150.bmp";
新线程(新的ImageDownloader(imageUrl)).start();
}
公共void showImageButton(视图v)
{
位图图像=ImageHandler.getImage();
if(image==null)
Log.e(“错误”,“无可用图像”);
否则{
ImageView ImageView=(ImageView)findViewById(R.id.ImageView);
设置图像位图(图像);
}
}
}
类ImageHandler
{
静态保护列表图像列表;
静态公共无效添加图像(位图图像)
{
imagesList.add(图像);
}
静态公共位图getImage()
{
如果(!imagesList.isEmpty())
返回imagesList.get(0);
其他的
返回null;
}
}
类ImageDownloader实现Runnable
{
公共医疗;
私有字符串urlString;
公共图像下载程序(字符串urlString)
{
this.urlString=urlString;
}
公开募捐
{
尝试
{
AndroidHttpClient=AndroidHttpClient.newInstance(“Android”);
HttpGet getRequest=新的HttpGet(urlString);
HttpResponse response=client.execute(getRequest);
HttpEntity=response.getEntity();
InputStream InputStream=null;
inputStream=(新的BufferedHttpEntity(entity)).getContent();
bmpImage=BitmapFactory.decodeStream(inputStream);
//bmpImage=BitmapFactory.decodeStream(新URL(urlString).openConnection().getInputStream());
}
捕获(例外e)
{
e(“ImageDownloadError”,e.toString());
返回;
}
如果(bmpImage!=null)
{
ImageHandler.addImage(bmpImage);
Log.i(“信息”,“图像下载成功”);
}
其他的
Log.e(“错误”,“位图为空”);
}
}
p、 s
showImageButton
抛出了IllegalStateException
,但我已经厌倦了它。我认为问题在于,一旦您使用了来自HttpUrlConnection的InputStream,就不能倒带并再次使用相同的InputStream。因此,您必须为图像的实际采样创建一个新的InputStream。否则我们必须中止http请求
只有当我们可以将inputstream用于httprequest时,如果您试图下载另一个映像,那么它将抛出一个错误,如“inputstream已创建”所示。因此,我们需要在使用httprequest.abort()下载后中止httprequest
使用以下命令:
HttpGet httpRequest = new HttpGet(URI.create(path) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();
根据我的观点,有两个原因
public class MainActivity extends Activity {
private ProgressDialog progressDialog;
private ImageView imageView;
private String url = "http://www.9ori.com/store/media/images/8ab579a656.jpg";
private Bitmap bitmap = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
Button start = (Button) findViewById(R.id.button1);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
progressDialog = ProgressDialog.show(MainActivity.this,
"", "Loading..");
new Thread() {
public void run() {
bitmap = downloadBitmap(url);
messageHandler.sendEmptyMessage(0);
}
}.start();
}
});
}
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
imageView.setImageBitmap(bitmap);
progressDialog.dismiss();
}
};
private Bitmap downloadBitmap(String url) {
// Initialize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = null;
try {
// getting contents from the stream
is = entity.getContent();
// decoding stream data back into image Bitmap
final Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
} finally {
if (is != null) {
is.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
getRequest.abort();
Log.e(getString(R.string.app_name), "Error "+ e.toString());
}
return null;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="Download Image" />
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerInside"
android:src="@drawable/ic_launcher" />
</LinearLayout>
另外,不要忘记在Manifest.xml文件中授予INTERNET权限我不确定您是否可以解码
.bmp
文件。。。编辑:也许这个问题的答案在这里会有所帮助@cyborg86pl我试着下载了jpg和png,但没有得到相同的结果。@cyborg86pl尝试了链接中的方法,仍然错误,但我得到的是“SkImageDecoder::Factory returned null”而不是“decoder->decode returned false”你能更新你的代码吗?当然。我一直在想,也许是因为我的jdk版本?我使用jdk8。仍然一样,返回null:(我真的不明白为什么会发生这种情况。你能分享你的布局文件(activity_main)代码吗?我想你需要在downloadButton上添加onClickListner。分享你的xml代码,这样我可以告诉你具体的原因。