我有問題與java類android上傳文件到遠程服務器。位圖的輸出始終顯示爲空的Android形式
在表單中,我選擇智能手機上的圖庫圖像上的文件,但位圖的輸出始終顯示爲空,並且表單開始被阻止。
Log.d("HomeActivity.class", "Output: " + bitmap);
這開始讓我相信我的整個結構是不正確的。
我錯過了什麼?
代碼有什麼問題?
我將不勝感激任何幫助,你可以給我解決這個問題。
public class HomeActivity extends Activity {
Button btnSend;
Spinner area;
EditText description;
ImageView viewImage;
Button b, upload;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
b = (Button) findViewById(R.id.btnSelectPhoto);
viewImage = (ImageView) findViewById(R.id.viewImage);
upload = (Button) findViewById(R.id.button1);
area = (Spinner) findViewById(R.id.my_spinner_new);
description = (EditText) findViewById(R.id.editText);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
upload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (area.getSelectedItem().toString().trim()
.equalsIgnoreCase("Select area")) {
Toast.makeText(HomeActivity.this, "Area.",
Toast.LENGTH_SHORT).show();
} else if (description.getText().toString().length() <= 0) {
description.setError("description");
} else if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"pic", Toast.LENGTH_SHORT)
.show();
Log.d("HomeActivity.class", "Output: " + bitmap);
} else {
ProgressDialog.show(HomeActivity.this,
"Uploading" + bitmap, "Please wait...", true);
}
}
});
new Thread() {
@Override
public void run() {
String path = "http://localhost/list.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u
.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[5120 * 512];
in.read(buffer);
bo.write(buffer);
String s = bo.toString();
final Vector<String> str = new Vector<String>();
String[] line = s.split("\n");
int index = 0;
while (index < line.length) {
str.add(line[index]);
index++;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Spinner spinner = (Spinner) findViewById(R.id.my_spinner_new);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
HomeActivity.this,
android.R.layout.simple_spinner_item, str);
spinner.setAdapter(adapter);
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
viewImage.setImageBitmap(bitmap);
Log.d("HomeActivity.class", "Valore restituito: " + bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage, filePath,
null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("path of image from gallery......******************.........",
picturePath + "");
viewImage.setImageBitmap(thumbnail);
}
}
}
}
編輯#1
upload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (area.getSelectedItem().toString().trim()
.equalsIgnoreCase("Select area")) {
Toast.makeText(HomeActivity.this, "Area.",
Toast.LENGTH_SHORT).show();
} else if (description.getText().toString().length() <= 0) {
description.setError("description");
} else if (bitmap == null) {
Toast.makeText(getApplicationContext(),
"pic", Toast.LENGTH_SHORT)
.show();
Log.d("HomeActivity.class", "Output: " + bitmap);
} else {
ProgressDialog.show(HomeActivity.this,
"Uploading" + bitmap, "Please wait...", true);
}
}
});
如果人們學會了如何使用調試器,95%的問題不會存在。爲了簡單的調試,它非常簡單。大概需要30分鐘。使用每種方法中帶有斷點的調試器逐步執行代碼。你會很快找到問題的根源。 – Simon 2014-09-23 09:29:25
謝謝,但我已經使用調試器,對於調試器我知道java類中的位圖爲空... – 2014-09-23 09:32:20
因此它在這裏是空的? 'bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);' – Simon 2014-09-23 09:58:22