我不能为我的生活直接从应用程序中获取图片。
目前,图片已保存到图库,无法显示,用户必须从图库中进行选择。
代码在下面,有人可以弄清楚我做错了吗?
总计Android新手并且已经坚持了好几个小时! Android如何才能更容易实现这样的通用功能? 叹息package com.tractable.imagegraph_dev.tractableapp;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.File;
public class MainActivity extends Activity {
private boolean hasActiveNetworkRequest = false;
private static final int SELECT_PICTURE = 0;
private NetworkUtils networkUtils = new NetworkUtils();
private GuiToolbelt guiToolbelt = new GuiToolbelt();
private ImageUploader imageUploader = new ImageUploader();
private ResponseParser responseParser = new ResponseParser();
private ImageUtils imageUtils = new ImageUtils();
private Toolbelt toolbelt = new Toolbelt();
private ImageView imageView;
private Bitmap bitmap;
private Bitmap dispBitmap;
private static double neededRatio = 1024.0 / 768.0;
private Button choosePhotoButton;
private Button takePhotoButton;
private TextView textView;
private String responseText;
private FrameLayout progressOverlay;
private ScrollView switchesScrollView;
private LinearLayout switchesLayout;
// Classifiers
private List<String> classifierNames = new ArrayList<String>();
private List<Switch> switches = new ArrayList<Switch>();
private String currentCategory = "";
private List<RadioButton> categoryButtons = new ArrayList<RadioButton>();
private RadioButton pipeButton;
private RadioButton carInsuranceButton;
private RadioButton dermatologyButton;
private Button runClassifiersButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchesLayout = (LinearLayout) findViewById(R.id.SwitchesLayout);
imageView = (ImageView) findViewById(R.id.icon);
choosePhotoButton = (Button) findViewById(R.id.uploadButton);
textView = (TextView) findViewById(R.id.messageText);
takePhotoButton = (Button) findViewById(R.id.takePhotoButton);
progressOverlay = (FrameLayout) findViewById(R.id.overlayFrame);
choosePhotoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pickPhoto(imageView);
}
});
takePhotoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
takePhoto(imageView);
}
});
pipeButton = (RadioButton) findViewById(R.id.pipesRadioButton);
carInsuranceButton = (RadioButton) findViewById(R.id.carInsuranceRadioButton);
dermatologyButton = (RadioButton) findViewById(R.id.dermatologyRadioButton);
categoryButtons.add(pipeButton);
categoryButtons.add(carInsuranceButton);
categoryButtons.add(dermatologyButton);
runClassifiersButton = (Button) findViewById(R.id.runClassifiersButton);
pipeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!hasActiveNetworkRequest) {
hasActiveNetworkRequest = true;
setupUiForCategory("pipes");
setImageUploadedText("");
pipeButton.setChecked(true);
}
}
});
carInsuranceButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!hasActiveNetworkRequest) {
hasActiveNetworkRequest = true;
setupUiForCategory("cars");
setImageUploadedText("");
carInsuranceButton.setChecked(true);
}
}
});
dermatologyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clearClassificationData();
setImageUploadedText("Classifier not available (currently training)");
}
});
runClassifiersButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (!hasActiveNetworkRequest) {
if (guiToolbelt.hasSelectedSwitch(switches)) {
progressOverlay.setVisibility(View.VISIBLE);
takePhotoButton.setEnabled(false);
choosePhotoButton.setEnabled(false);
uploadPhoto(imageView);
} else {
setImageUploadedText("Please select at least one classifer");
}
}
}
});
}
public void pickPhoto(View view) {
if (!currentCategory.equals("")) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
} else if (currentCategory.equals("")) {
setImageUploadedText("Please select a category");
}
}
public void takePhoto(View view) {
if (!currentCategory.equals("")) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent.createChooser(intent, "Take Picture"), 0);
} else if (currentCategory.equals("")) {
setImageUploadedText("Please select a category");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
if (networkUtils.isNetworkConnected(this.getApplicationContext())) {
try {
try {
Uri s1 = data.getData();
System.out.println("-+-+-1" + s1 + "-" + data);
InputStream s2 = getContentResolver().openInputStream(s1);
System.out.println("-+-+-2");
bitmap = BitmapFactory.decodeStream(s2);
System.out.println("-+-+-try" + bitmap.getWidth() + " " + bitmap.getHeight());
} catch (Exception e) {
e.printStackTrace();
bitmap = (Bitmap) data.getExtras().get("data");
System.out.println("-+-+-catch" + bitmap.getWidth() + " " + bitmap.getHeight());
}
int neededHeight = bitmap.getHeight();
if (bitmap.getHeight() > bitmap.getWidth())
neededHeight = (int) (bitmap.getWidth() / neededRatio);
int neededWidth = (int) (neededHeight * neededRatio);
// Setup Image Data
bitmap = ThumbnailUtils.extractThumbnail(bitmap, neededWidth, neededHeight);
//dispBitmap = Bitmap.createBitmap(bitmap);
//if (dispBitmap.getWidth() < dispBitmap.getHeight()) {
// dispBitmap = imageUtils.rotateImage(dispBitmap);
//}
//if (dispBitmap.getWidth() > Constants.scaledImagePixels || dispBitmap.getHeight() > Constants.scaledImagePixels) {
// dispBitmap = imageUtils.scaleImageWidth(dispBitmap, Constants.scaledImagePixels);
//} else if (dispBitmap.getHeight() < Constants.minimumImagePixels || dispBitmap.getWidth() < Constants.minimumImagePixels) {
// dispBitmap = imageUtils.scaleImageWidth(dispBitmap, Constants.scaledImagePixels);
//}
imageView.setImageBitmap(bitmap);
runClassifiersButton.setEnabled(true);
} catch (Exception e) {
e.printStackTrace();
setImageUploadedText("There was an error getting the image");
}
} else {
setImageUploadedText("Please connect to the internet to use the app.");
progressOverlay.setVisibility(View.GONE);
}
}
public void uploadPhoto(View view) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
processImageUpload();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
textView.setText("Uploading...");
}
public void setupUiForCategory(final String category) {
clearClassificationData();
setCurrentCategory(category);
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
classifierNames = ClassifierDelegate.getInstance().getClassifersListForCategory(category);
runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < classifierNames.size(); i++) {
Switch newSwitch = new Switch(getBaseContext());
newSwitch.setText(classifierNames.get(i));
switches.add(newSwitch);
switchesLayout.addView(newSwitch);
}
guiToolbelt.setSwitchAsSelectedByString(switches, pipeButton.getText().toString());
hasActiveNetworkRequest = false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
private void clearClassificationData() {
setCurrentCategory("");
classifierNames.clear();
guiToolbelt.setAllRadioButtions(categoryButtons, false);
for (int i = 0; i < switches.size(); i++) {
switches.get(i).setText("");
}
switches.clear();
switchesLayout.removeAllViewsInLayout();
}
public void processImageUpload() throws Exception {
responseText = imageUploader.uploadImage(Constants.serverUrl, bitmap, getClassifiersList(), currentCategory);
if (responseText != "") {
// Map<String, Double> parsedResponse = responseParser.parseMapResponse(response);
this.runOnUiThread(new Runnable() {
@Override
public void run() {
setImageUploadedText(responseText.replace(";", "\n"));
progressOverlay.setVisibility(View.GONE);
takePhotoButton.setEnabled(true);
choosePhotoButton.setEnabled(true);
}
});
}
}
private List getClassifiersList() {
List<String> classifiersList = new ArrayList<String>();
for (int i = 0; i < switches.size(); i++) {
if (switches.get(i).isChecked()) {
classifiersList.add(switches.get(i).getText().toString().toLowerCase().replace(" ", ""));
}
}
return classifiersList;
}
void setImageUploadedText(String text) {
textView.setText(text);
}
void setCurrentCategory(String catergory) { currentCategory = catergory; }
}
顺便说一下,我正在为Moto 4G编写这个应用程序。
答案 0 :(得分:0)
public static final int CAMERA_REQUEST = 1980;
Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentcamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentcamera, CAMERA_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
your_screenview.setImageBitmap(photo);