download image and swap image

http://blog.vogella.com/2011/06/14/android-downloadmanager-example/

 

code:

package com.quickplaysport.download2;

import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
private long enqueue;
private DownloadManager dm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 

// this broadcast reveiver means it has to be on button click
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);

// if the download successfull then swap the image
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {

ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
view.setImageURI(Uri.parse(uriString));

Uri myDownloadedFile = dm.getUriForDownloadedFile(downloadId);
}
}
}
}
};

registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse(“http://www.wetterzentrale.de/pics/MS_644_ens.png”));
enqueue = dm.enqueue(request);

}

public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}

// try getting the uri of the downloaded file and setting it to a textview
}

reading data from internal storage

public void read(View view){
String myFilename = “freekick”;
String readData = “”;

openFile(myFilename);

}

public  void openFile(String myFilename) {
String value = “”;
FileInputStream fis;

try {
fis = openFileInput(myFilename);
byte[] input = new byte[fis.available()];
while(fis.read(input) != -1){
value += new String(input);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
TextView myTextView = (TextView) findViewById(R.id.textView);
myTextView.setText(value);
}
}

 

Setting an image views background image from java

within some function

 

// get the view that will be changed
        ImageView myImageView = (ImageView)findViewById(R.id.myImageView);
        Drawable new_image= getResources().getDrawable(R.drawable.changed);
        myImageView.setBackgroundDrawable(new_image);

 

breakdown:

the first line:

     ImageView myImageView = (ImageView)findViewById(R.id.myImageView);

ImageView is creating a reference object of type Imageview

myImageView is the variable name for the new reference object

(ImageView) is a caster i.e. what the data will be put into

findViewById() gets the View referenced in the brackets

 

the second line:

Drawable new_image= getResources().getDrawable(R.drawable.changed);

Drawable – the type of reference object we’re creating

new_image – my name for the reference object

getResources().getDrawable(R.drawable.changed);

gets the object to reference from the repository

 

third line:

        myImageView.setBackgroundDrawable(new_image);

myImageView – my reference object

setBackgroundDrawable(new_image); – command to change the background image on the object referenced by myImageView, it changes it to new_image

reading data from tag

to read the data from a views tag use this code in bold:

public void passdata(View view) {

 String tag = (String) view.getTag();

 

*the first String defines the variable type

 tag is just the name of my variable

(String) – this is a caster it is the variable type the data from the tag is cast into

view in the example is the name of the view from the start of the function.

 

No need for anything in the brackets as it knows which tag to get because it is the tag that invoked the function

 

Setting text to a view from within an action

To change the text within a view from the java class do this:

 

// declairing the text view to change

        TextView textElement = (TextView) findViewById(R.id.textView);
        textElement.setText(easyPuzzle);

//leave this line to assign a specific text

 

Alternatively use this to change it to a string from the resources folder
    //textElement.setText(R.string.my_string_name);

//leave this line to assign a string resource

Passing data to an action

To send data from one action to another you need to use extras on intents

 

In the first action add this

public void passdata(View view) {
String myText = “Some Text”;
// Do something in response to button
Intent myIntent = new Intent(this, Two.class);
  myIntent.putExtra(“name_of_data_to_retreive”, myText (The string or variable to send);
startActivity(myIntent);

}

 

Then in the action you need the data in onCreate.

Intent intent = getIntent();
        String MyVariable= intent.getExtras().getString(“name_of_data_to_retreive“);

 

The string is now inside MyVariable

 

Now see setting the text within a view for one use of this method