我尝试从以下地址下载图片文件:http://i0.kym-cdn.com/photos/images/newsfeed/000/002/110/longcat.jpg
我使用了以下设置:
MainActivity.java
:
public class MainActivity
extends ActionBarActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
}
PlaceholderFragment.java
:
public class PlaceholderFragment
extends Fragment
{
private Button button;
public PlaceholderFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
button = (Button)rootView.findViewById(R.id.fragment_main_button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new ImageDownloadAsyncTask().execute("http://i0.kym-cdn.com/photos/images/newsfeed/000/002/110/longcat.jpg");
}
});
return rootView;
}
public class ImageDownloadAsyncTask extends AsyncTask<String, Void, byte[]>
{
@Override
protected byte[] doInBackground(String... params)
{
if(params.length <= 0)
{
return null;
}
byte[] imageData = null;
String url = params[0];
HttpURLConnection httpURLConnection = null;
try
{
URL address = new URL(url);
httpURLConnection = (HttpURLConnection)address.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
if(httpURLConnection.getResponseCode() == 200)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(httpURLConnection.getInputStream(), byteArrayOutputStream);
imageData = byteArrayOutputStream.toByteArray();
}
else
{
return null;
}
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
if(httpURLConnection != null)
{
httpURLConnection.disconnect();
}
}
return imageData;
}
@Override
protected void onPostExecute(byte[] bytes)
{
super.onPostExecute(bytes);
if(bytes != null) {
ImageDisplayDialogFragment imageDisplayDialogFragment = new ImageDisplayDialogFragment();
imageDisplayDialogFragment.setTargetFragment(PlaceholderFragment.this, 0);
Bundle bundle = new Bundle();
bundle.putByteArray("imageData", bytes);
imageDisplayDialogFragment.setArguments(bundle);
imageDisplayDialogFragment.show(getActivity().getSupportFragmentManager(), ImageDisplayDialogFragment.TAG);
} else {
Toast.makeText(getActivity(), R.string.downloading_file_failed, Toast.LENGTH_LONG).show();
}
}
}
}
ImageDisplayDialogFragment.java
:
public class ImageDisplayDialogFragment extends DialogFragment
{
private ImageView imageView;
public static final String TAG = ImageDisplayDialogFragment.class.getName();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.dialogfragment_imagedisplay, container, false);
imageView = (ImageView)view.findViewById(R.id.dialog_imagedisplay_imageview);
byte[] imageData = getArguments().getByteArray("imageData");
Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
imageView.setMinimumWidth(bmp.getWidth());
imageView.setMinimumHeight(bmp.getHeight());
imageView.setImageBitmap(bmp);
return view;
}
}
从技术上讲,我只是使用AsyncTask
使用commons-io
将URL的内容下载到字节数组中,然后我将字节数组发送到对话框片段以在ImageView的。
对话框片段的XML布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/dialog_imagedisplay_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/longcat"
android:layout_centerInParent="true"/>
</RelativeLayout>
然而,它在图像上方显示一个奇怪的青色线条神器,而ImageView也比实际图像本身大。
图像:
我哪里可能出错?
答案 0 :(得分:1)
根据您提供的屏幕截图以及事件DialogFragment
,我可以安全地假设蓝线/空格是对话框的标题栏(类似,但与ActionBar略有不同) 。为了禁用它,有一个内置方法:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
这将禁用DialogFragment的标题栏。
有关详细信息,请参阅docs。