I am making an app that basically lets a user enter a url along with a title in an edit text and then by pressing a download button, the image will display on another activity. There are some other functions as well that aren't relevant right now. The problem I am having is that when I put in the id for the imageview from where the downloaded image should be displayed and press download for an image the app crashes. This is my code so far:
Main Activity:
package com.example.faraz.assignment2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dl(View view) {
Intent intent = new Intent(this,DownloadActivity.class);
startActivity(intent);
}
public void viewA(View view) {
Intent viewPic = new Intent(this,ViewActivity.class);
startActivity(viewPic);
}
}
Main XML:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center_vertical|center_horizontal|center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"
android:onClick="dl"/>
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View"
android:onClick="viewA"/>
<Button
android:id="@+id/range"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Range" />
</android.widget.LinearLayout>
ViewActivity is where I am trying to display the image:
package com.example.faraz.assignment2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
}
}
ViewActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center_vertical|center_horizontal|center"
android:orientation="vertical"
tools:context=".DownloadActivity">
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars[0]" />
</android.widget.LinearLayout>
DownloadActivity is where the image is being downloaded from:
package com.example.faraz.assignment2;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownloadActivity extends AppCompatActivity {
private Button btn;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
btn = (Button)findViewById(R.id.buttonD);
et = (EditText)findViewById(R.id.link);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
URL url = new URL(et.getText().toString());
new MyDownloadTask().execute(url);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private class MyDownloadTask extends AsyncTask<URL, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(URL... params) {
URL url = params[0];
Bitmap bitmap = null;
try {
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
ImageView myImage = (ImageView) findViewById(R.id.test);//error is here when I change id for imageview to the one in ViewActivity
myImage.setImageBitmap(bitmap);
} else {
Toast.makeText(getApplicationContext(), "Failed to Download
Image", Toast.LENGTH_LONG).show();
}
}
}
}
The code for downloadactivity was taken from here
DownloadActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear_layout"
android:gravity="top|center_vertical|center_horizontal|center"
android:orientation="vertical"
tools:context=".DownloadActivity">
<EditText
android:id="@+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="URL"
android:inputType="textPersonName" />
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName" />
<Button
android:id="@+id/buttonD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download" />
<ImageView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
</android.widget.LinearLayout>
I would appreciate the help.
Edit: This is my DownloadActivity now but it still wont show the picture on ViewActivity:
btn = (Button)findViewById(R.id.buttonD);
et = (EditText)findViewById(R.id.link);
pic = (ImageView) findViewById(R.id.pic);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String url = et.getText().toString();
Glide.with(DownloadActivity.this).load(url).into(pic);
} catch (Exception e) {
e.printStackTrace();
}
}
});