안드로이드 개발 질문/답변
(글 수 45,052)
package com.url;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
public class url extends Activity {
public static File mfile = null;
private ProgressBar mProgress;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private Handler mHandler = new Handler();
private int mProgressStatus = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
URL text = null;
try {
text = new URL("http://upload.wikimedia.org/wikipedia/commons/a/a8/Rembrandt-A-Lion-Lying-Down-207063.jpg");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection http = null;
try {
http = (HttpURLConnection)text.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
inputStream = text.openStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
File filedir = getFilesDir();
mfile = new File(filedir, "myFile.jpg");
//if(!file.exists())
mfile.delete();
try {
mfile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
outputStream = new FileOutputStream(mfile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
mProgress = new ProgressDialog(null);
mProgress.setTitle("Loding...");
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMax(http.getContentLength()/2);
mProgress.show();
*/
mProgress = (ProgressBar) findViewById(R.id.ProgressBar01);
mProgress.setMax(http.getContentLength());
new Thread(new Runnable() {
public void run() {
try {
while ((mProgressStatus=inputStream.read())!= -1) {
try {
synchronized (this) {
outputStream.write(mProgressStatus);
outputStream.flush();
}
} catch (Exception e) {
Log.e("wait failed", e.toString());
}
mProgressStatus=mProgressStatus+mProgressStatus/5;
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
setContentView(new ViewWithBitmap(this));
}
private static class ViewWithBitmap extends View {
public ViewWithBitmap(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.BLACK);
//bmp,jpg read sucess, no read jp2(jpeg2000 format)
String path = mfile.getAbsolutePath();
Bitmap pic = BitmapFactory.decodeFile(path);
if(pic == null) {
return;
}
canvas.drawBitmap(pic, 0, 0, null);
super.onDraw(canvas);
}
}
/*
private boolean inputStream() throws IOException {
URL text = new URL("http://upload.wikimedia.org/wikipedia/commons/a/a8/Rembrandt-A-Lion-Lying-Down-207063.jpg");
HttpURLConnection http = (HttpURLConnection)text.openConnection();
inputStream = text.openStream();
File filedir = getFilesDir();
mfile = new File(filedir, "myFile.jpg");
//if(!file.exists())
mfile.delete();
mfile.createNewFile();
outputStream = new FileOutputStream(mfile);
mProgress = new ProgressDialog(null);
mProgress.setTitle("Loding...");
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMax(http.getContentLength()/2);
mProgress.show();
mProgress = (ProgressBar) findViewById(R.id.ProgressBar01);
mProgress.setMax(http.getContentLength());
new Thread(new Runnable() {
public void run() {
try {
while ((mProgressStatus=inputStream.read())!= -1) {
try {
synchronized (this) {
outputStream.write(mProgressStatus);
outputStream.flush();
}
} catch (Exception e) {
Log.e("wait failed", e.toString());
}
mProgressStatus=mProgressStatus+mProgressStatus/5;
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
try{
//Do some Fake-Work
while((mProgressStatus=inputStream.read())!= -1) {
outputStream.write(mProgressStatus);
outputStream.flush();
//mProgress.incrementProgressBy(mProgressStatus/2);
}
// Dismiss the Dialog
//mProgress.dismiss();
}catch(Exception e){
}
return true;
}
*/
}
풀 소스입니다.
프로그래스바를 띄워서 로딩화면을 만들려고 하는데요,
하루종일 매달려도 되질 않네요.
오류 메세지는 없고 실행시 The application url(processcom.url) has stopped unexpectedly. Please try again. 이라고 나옵니다.
고수님들의 조언 부탁드려요.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
public class url extends Activity {
public static File mfile = null;
private ProgressBar mProgress;
private InputStream inputStream = null;
private OutputStream outputStream = null;
private Handler mHandler = new Handler();
private int mProgressStatus = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
URL text = null;
try {
text = new URL("http://upload.wikimedia.org/wikipedia/commons/a/a8/Rembrandt-A-Lion-Lying-Down-207063.jpg");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection http = null;
try {
http = (HttpURLConnection)text.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
inputStream = text.openStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
File filedir = getFilesDir();
mfile = new File(filedir, "myFile.jpg");
//if(!file.exists())
mfile.delete();
try {
mfile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
outputStream = new FileOutputStream(mfile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
mProgress = new ProgressDialog(null);
mProgress.setTitle("Loding...");
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMax(http.getContentLength()/2);
mProgress.show();
*/
mProgress = (ProgressBar) findViewById(R.id.ProgressBar01);
mProgress.setMax(http.getContentLength());
new Thread(new Runnable() {
public void run() {
try {
while ((mProgressStatus=inputStream.read())!= -1) {
try {
synchronized (this) {
outputStream.write(mProgressStatus);
outputStream.flush();
}
} catch (Exception e) {
Log.e("wait failed", e.toString());
}
mProgressStatus=mProgressStatus+mProgressStatus/5;
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
setContentView(new ViewWithBitmap(this));
}
private static class ViewWithBitmap extends View {
public ViewWithBitmap(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.BLACK);
//bmp,jpg read sucess, no read jp2(jpeg2000 format)
String path = mfile.getAbsolutePath();
Bitmap pic = BitmapFactory.decodeFile(path);
if(pic == null) {
return;
}
canvas.drawBitmap(pic, 0, 0, null);
super.onDraw(canvas);
}
}
/*
private boolean inputStream() throws IOException {
URL text = new URL("http://upload.wikimedia.org/wikipedia/commons/a/a8/Rembrandt-A-Lion-Lying-Down-207063.jpg");
HttpURLConnection http = (HttpURLConnection)text.openConnection();
inputStream = text.openStream();
File filedir = getFilesDir();
mfile = new File(filedir, "myFile.jpg");
//if(!file.exists())
mfile.delete();
mfile.createNewFile();
outputStream = new FileOutputStream(mfile);
mProgress = new ProgressDialog(null);
mProgress.setTitle("Loding...");
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMax(http.getContentLength()/2);
mProgress.show();
mProgress = (ProgressBar) findViewById(R.id.ProgressBar01);
mProgress.setMax(http.getContentLength());
new Thread(new Runnable() {
public void run() {
try {
while ((mProgressStatus=inputStream.read())!= -1) {
try {
synchronized (this) {
outputStream.write(mProgressStatus);
outputStream.flush();
}
} catch (Exception e) {
Log.e("wait failed", e.toString());
}
mProgressStatus=mProgressStatus+mProgressStatus/5;
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
try{
//Do some Fake-Work
while((mProgressStatus=inputStream.read())!= -1) {
outputStream.write(mProgressStatus);
outputStream.flush();
//mProgress.incrementProgressBy(mProgressStatus/2);
}
// Dismiss the Dialog
//mProgress.dismiss();
}catch(Exception e){
}
return true;
}
*/
}
풀 소스입니다.
프로그래스바를 띄워서 로딩화면을 만들려고 하는데요,
하루종일 매달려도 되질 않네요.
오류 메세지는 없고 실행시 The application url(processcom.url) has stopped unexpectedly. Please try again. 이라고 나옵니다.
고수님들의 조언 부탁드려요.