我试图在Android Studio中创建一个应用程序,允许用户使用EditText并输入图像的URL,然后图像显示在imageview中
package com.example.michelle.imageurl;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Image extends Activity {
Button retrieveImage;
ImageView image;
String fileUrl, src;
Bitmap bitmap;
ProgressDialog pDialog;
/*Drawable loadImagefromNetwork(String url){
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src.name");
return d;
}
catch(Exception e)
{
System.out.println("Exc=" + e);
return null;
}
}*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
retrieveImage = (Button)findViewById(R.id.retrieve);
image = (ImageView)findViewById(R.id.imageView);
loadImageFromURL();
getBitmapFromURL(src);
retrieveImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new LoadImage().execute(fileUrl);
}
});
}
public Bitmap getBitmapFromURL(String src)
{
try
{
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private class LoadImage extends AsyncTask<String, String, Bitmap>
{
@Override
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(Image.this);
pDialog.setMessage("Loading Image...");
pDialog.show();
}
}
protected void onPostExecute(Bitmap image) {
if(image != null){
ImageView img = new ImageView(this);
img.setImageBitmap(bitmap);
pDialog.dismiss();
}else{
pDialog.dismiss();
Toast.makeText(Image.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_image, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean loadImageFromURL(){
try {
URL myFileUrl = new URL (fileUrl);
HttpURLConnection conn =
(HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
image.setImageBitmap(BitmapFactory.decodeStream(is));
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
这是我的主要活动的代码,我在那里使用ImageView和位图以及尝试获取图像的所有内容但是当我运行应用程序时,我得到的是一个不变的弹出窗口只是说加载图像图像从未实际加载
这是活动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"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Image">
<EditText
android:layout_width= "150dp"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_marginTop="90dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrieve Image"
android:id="@+id/retrieve"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="100dp"
android:layout_height="50dp"
android:id="@+id/imageView"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
</LinearLayout>
非常感谢任何帮助
答案 0 :(得分:1)
方法protected void onPostExecute(Bitmap image)
需要在私有类LoadImage中。看起来你只是在内部类定义之外。
答案 1 :(得分:0)
你可以使用Glide库。
retrieveImage = (Button) findViewById(R.id.retrieve);
imageView = (ImageView) findViewById(R.id.imageView);
final EditText editText = (EditText) findViewById(R.id.image);
retrieveImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Glide.with(Main22Activity.this).load(editText.getText().toString()).into(imageView);
}
});
添加这些依赖项
compile&#39; com.github.bumptech.glide:glide:3.6.1&#39;
编译&#39; com.android.support:support-v4:19.1.0&#39;
答案 2 :(得分:0)
使用picasso library非常简单易用,非常适合您的任务
Picasso.with(context).load(EditText.getText().toString).into(imageView);
将库添加到build.gradle
compile 'com.squareup.picasso:picasso:2.5.2'