![](/img/trans.png)
[英]Downloading image from Firebase storage into imageview on android studio java
[英]Cyan bar appears above ImageView after downloading image from website in DialogFragment
我嘗試從以下地址下載圖像文件: 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也大於實際圖像本身。
圖片:
我哪里出問題了?
從提供的屏幕截圖以及它是DialogFragment
的事實DialogFragment
,我可以安全地假定藍線/空格是對話框的標題欄(與ActionBar相似,但略有不同)。 為了禁用此功能,有一個內置方法:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
這將禁用DialogFragment的標題欄。
有關更多信息,請參閱文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.