我正在尝试将所有文本从url文本文件读取到字符串,但我遇到了这些错误:
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
这是我的代码:
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) {
}
}
};
}
............................................... .................................................. .................................................. ..................
答案 0 :(得分:0)
据我所知,currentUrl实际上并未在Target中定义。
String fileName = currentUrl.substring(
currentUrl.lastIndexOf('/') + 1,
currentUrl.length());
这里定义的currentUrl在哪里?它似乎在onCreate中定义,但没有在其他地方定义。
答案 1 :(得分:0)
这是一个简单的Java范围问题。您已在try
的{{1}}内声明了您的变量,因此onCreate()
块内部是唯一可以访问的地方。
如果要在其他区域使用它,则需要将其声明为成员变量。
try