我无法从我的网址下载图片,但是说找不到协议,你可以在我的屏幕截图HttpURLConnection Error Message中看到 该网址附加了“http://”。我已经阅读并尝试了本网站中的所有其他解决方案,但无济于事。
这是我目前的工作来源。
public class MainActivity extends AppCompatActivity {
DBHandler myDB;
public String defaultURL = "http://ae1230ohx8.smartrelease.jp/eflends/"; // Default URL
private WebView webView;
private String selectedLinkTitle; //link that was passed from FavoriteList
private String searchedURL; // search URL from MainMenu (Search field)
Map<String,String> listFavoriteDataDB;//links and id from Favorites table DB
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebViewClient mWebViewClient = new WebViewClient();
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(mWebViewClient);
myDB = new DBHandler(this);
final ImageView favoriteBtn = (ImageView)findViewById(R.id.favorite);
final ImageView menuBtn = (ImageView)findViewById(R.id.mainmenu);
final ImageView backBtn = (ImageView)findViewById(R.id.backMainView);
getDBData(); //get the data
//set the onclick listener for favorite btn
favoriteBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick( View v )
{
String webUrl = webView.getUrl();
String webUrlTitle = webView.getTitle();
Boolean isFavorite = isFave(webUrl);
if(webUrl!="") {
if(isFavorite.equals(false)){
myDB.insertData(webUrl, webUrlTitle);
Toast.makeText(getApplicationContext(), "お気に入りに追加されました。", Toast.LENGTH_LONG).show();
favoriteBtn.setImageResource(R.drawable.favorites_added);
webView.evaluateJavascript("document.head.querySelector(\"[property='og:image']\").content", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
getBitmapFromURL(s);
}
});
} else {
if(webUrl.contains("/lens/")) {
if (webUrl.contains("ef-l")) {
favoriteBtn.setImageResource(R.drawable.favorites_notadded_white);
} else {
favoriteBtn.setImageResource(R.drawable.favorites_notadded);
}
} else {
favoriteBtn.setImageResource(R.drawable.favorites_notadded);
}
deleteFavorite(webUrlTitle);
}
}
}
});
//set the onclick listener for menu btn
menuBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick( View v )
{
String webUrl = webView.getUrl();
Intent intent = new Intent(MainActivity.this, MainMenu.class);
intent.putExtra("currentURL",webUrl);
startActivity(intent);
overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);
}
});
//set the onclick listener for back button
backBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick( View v )
{
if(webView.canGoBack())
webView.goBack();
}
});
loadWeb();
}
public void loadWeb(){
// //dynamically sets the button if the url is found in the db
webView.setWebViewClient(new WebViewClient(){
final ImageView favoriteBtn = (ImageView)findViewById(R.id.favorite);
final ImageView menuBtn = (ImageView)findViewById(R.id.mainmenu);
final ImageView logo = (ImageView)findViewById(R.id.logo);
final Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar3);
final TextView border = (TextView)findViewById(R.id.border);
final ImageView backBtn = (ImageView)findViewById(R.id.backMainView);
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.proceed("eflends", "test");
}
public void onPageStarted(WebView view, String url, Bitmap favicon){
favoriteBtn.setEnabled(true);
if(url.contains("/lens/")) {
if (url.contains("ef-l")) {
toolbar.setBackground(new ColorDrawable(Color.BLACK)); //make the bar black
menuBtn.setImageResource(R.drawable.main_menu_white);
logo.setImageResource(R.drawable.logo_canon01);
border.setBackgroundColor(Color.parseColor("#444444"));
backBtn.setImageResource(R.drawable.icon_arrow_left_white);
favoriteBtn.setImageResource(R.drawable.favorites_notadded_white);
} else {
toolbar.setBackground(new ColorDrawable(Color.WHITE)); //make it white
menuBtn.setImageResource(R.drawable.mainmenu);
logo.setImageResource(R.drawable.logo_canon02);
border.setBackgroundColor(Color.parseColor("#f0f0f0"));
backBtn.setImageResource(R.drawable.icon_arrow_left);
favoriteBtn.setImageResource(R.drawable.favorites_notadded);
}
} else {
toolbar.setBackground(new ColorDrawable(Color.WHITE)); //make it white for all
menuBtn.setImageResource(R.drawable.mainmenu);
logo.setImageResource(R.drawable.logo_canon02);
border.setBackgroundColor(Color.parseColor("#f0f0f0"));
backBtn.setImageResource(R.drawable.icon_arrow_left);
favoriteBtn.setImageResource(R.drawable.favorites_notadded);
}
if(webView.canGoBack()){
backBtn.setVisibility(View.VISIBLE);
} else {
backBtn.setVisibility(View.INVISIBLE);
}
//check if url already exist.
if(listFavoriteDataDB != null && listFavoriteDataDB.containsValue(url)){
favoriteBtn.setImageResource(R.drawable.favorites_added);
}
}
public void onPageFinished(WebView view, String url){
if(webView.canGoBack()){
backBtn.setVisibility(View.VISIBLE);
} else {
backBtn.setVisibility(View.INVISIBLE);
}
}
});
loadURL();
}
public boolean isFave(String webUrl){
listFavoriteDataDB = new HashMap<String, String>();
Cursor data = myDB.isFavorite(webUrl);
int rowCount = data.getCount();
if(rowCount != 0){
return true;
}
return false;
}
public void getDBData(){
//get the urls from the db
listFavoriteDataDB = new HashMap<String, String>();
Cursor data = myDB.getAllData();
while(data.moveToNext()) {
listFavoriteDataDB.put(data.getString(1), data.getString(2)); //put title(key) and url(value)
}
}
public void loadURL() {
webView.loadUrl(defaultURL);
}
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public void onResume(){
super.onResume();
Window decorView = getWindow();
// Hide the status bar.
decorView.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Calls the menu again.
// invalidateOptionsMenu();
Log.e("APP :", "onResume");
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String link = "";
Bundle extras = intent.getExtras();
//get the name that was passed
selectedLinkTitle = extras.getString("title");
searchedURL = extras.getString("url");
String menuLink = extras.getString("linkFromMenu");
//check if there is a passed value from favorite activity
if (selectedLinkTitle!=null || searchedURL!=null || menuLink!= null) {
if(selectedLinkTitle != null) {
link = listFavoriteDataDB.get(selectedLinkTitle);
Log.e("APP :", "loadURL - favorites");
} else if (searchedURL != null){
link = searchedURL.toString();
Log.e("APP :", "loadURL - search");
} else if (menuLink != null){
link = menuLink.toString();
Log.e("APP :", "load -" + menuLink);
}
}
webView.loadUrl(link);
Log.e("APP :", "onNewIntent");
}
public void deleteFavorite(String title) {
int itemID = getItemID(title);
myDB.deleteFavoriteURL(itemID,title);
Toast.makeText(getApplicationContext(), "削除されました。", Toast.LENGTH_SHORT).show();
}
public int getItemID(String title){
Cursor data = myDB.getItemID(title); //get the itemID from Title
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0); //get the ID
}
if(itemID > -1) {
return itemID;
}else{
return itemID;
}
}
public void getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Toast.makeText(getApplicationContext(), imageUrl, Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
如果您粘贴源代码,会更容易为您提供帮助。
但我已经不得不在不使用毕加索的情况下下载图像,这里是我使用过的AsyncTask:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import br.com.soutsapp.Souts_v3.R;
public class ImageDonwloaderTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public ImageDonwloaderTask(ImageView imageView){
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
return getBitmapFromURL(params[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
ImageView imageView = imageViewReference.get();
if (imageView != null ) {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
else {
imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.mipmap.ic_launcher));
}
}
}
public Bitmap getBitmapFromURL(String src) {
Bitmap myBitmap = null;
try {
if(src != null){
String newUrl = src.replace(" ", "%20");
java.net.URL url = new java.net.URL(newUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
}
} catch (IOException e) {
e.printStackTrace();
Log.e("SOUTS", e.getLocalizedMessage());
}
return myBitmap;
}
}