0

i am trying to read all texts from url text file to string but i am having these errors:

Description Resource    Path    Location    Type
currentUrl cannot be resolved to a variable MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 51 Java Problem
currentUrl cannot be resolved   MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 62 Java Problem
currentUrl cannot be resolved   MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 64 Java Problem
currentUrl cannot be resolved   MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 63 Java Problem

this is my code:

package com.example.imagedownloadsample;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_download);

        //
        try {

            URL url = new URL(
                    "https://www.dropbox.com/s/s70xckuxjh5sbtw/pop.txt");

            // read text returned by server
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    url.openStream()));

            String currentUrl;
            while ((currentUrl = in.readLine()) != null) {
                System.out.println(currentUrl);
            }
            in.close();

        } catch (MalformedURLException e) {
            System.out.println("Malformed URL: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
        //

        Picasso.with(this).load(currentUrl).into(target);

    }

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    String fileName = currentUrl.substring(
                            currentUrl.lastIndexOf('/') + 1,
                            currentUrl.length());

                    String fname = "/image-" + fileName + ".jpg";

                    File file = new File(Environment
                            .getExternalStorageDirectory().getPath() + fname);
                    if (file.exists())
                        file.delete();
                    try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 100, ostream);
                        ostream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {
            }
        }
    };

}

.....................................................................................................................................................................

2 Answers2

0

As far as I can tell, currentUrl isn't actually defined in Target.

 String fileName = currentUrl.substring(
                            currentUrl.lastIndexOf('/') + 1,
                            currentUrl.length());

Where is currentUrl defined here? It seems to be defined in onCreate, but no where else.

Andrew T.
  • 4,598
  • 4
  • 35
  • 54
0

It's a simple Java scope issue. You have your variable declared inside the try in onCreate() so inside that try block is the only place it is accessible.

If you want to use it in other areas then you will need to declare it as a member variable.

//declare it here
String currentUrl = "";

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_download);

    //
    try {

        URL url = new URL(
                "https://www.dropbox.com/s/s70xckuxjh5sbtw/pop.txt");

        // read text returned by server
        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream()));

Java docs

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • ur right but how do i send value from try block to member variable. what i want is i want to send the value that is i am getting from url text file to member variable – android question Aug 12 '14 at 15:44
  • You can use [StringBuilder](http://stackoverflow.com/questions/12899953/in-java-how-to-append-a-string-more-efficiently) – codeMagic Aug 12 '14 at 15:47