3

我正在创建一个 Android 应用程序,它拍摄照片并将其发送到 Ocr Web 服务(Microsoft Hawaii);但是我遇到了一个很糟糕的问题.. 拍完照片后,应用程序崩溃了,但我不明白问题出在哪里,以及如何解决!代码是这样的:

package your.Mobile;

 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.location.GpsStatus;
 import android.location.GpsStatus.Listener;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.MediaStore;
 import android.util.Log;

 import android.view.View;
 import android.widget.ImageView;

 public class FotocameraActivity extends Activity {


// dati che servono per la fotocamera
private static final int CAMERA_REQUEST = 100; // un numero a nostro
                                                                                                // piacimento
File tmpFotoFile = null;
byte[] bitmapdata;
ImageView preview;
LocationManager locationManager;
String gps;
final static String ARRAY_BYTE = "ARRAY_BYTE";
final static String GPS = "GPS";

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            startGpsTracking();
        }

        try {
                launchCamera();
        } catch (IOException e) {
                throw new RuntimeException(e);
        }
}

private void startGpsTracking() {
    // TODO Auto-generated method stub
    locationManager.addGpsStatusListener(gpsListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 3, myLocationListener);
}

private Listener gpsListener = new Listener(){
    public void onGpsStatusChanged(int status){
        switch(status){
        case GpsStatus.GPS_EVENT_FIRST_FIX:
            Log.d(LOCATION_SERVICE,"onGpsStatusChanged First Fix");
            break;
        case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            Log.d(LOCATION_SERVICE, "onGpsStatusChanged Satellite");
            break;
        case GpsStatus.GPS_EVENT_STARTED:
            Log.d(LOCATION_SERVICE, "onGpsStatusChanged Started");
            break;
        case GpsStatus.GPS_EVENT_STOPPED:
            Log.d(LOCATION_SERVICE, "onGpsStatusChanged Stopped");
            break;
        }
    }
};

private LocationListener myLocationListener = new LocationListener(){

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        double lng = location.getLongitude(), lat = location.getLatitude();
        gps += String.format("%6f", lng) + "#" + String.format("%6f", lat); 

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

};

private void launchCamera() throws IOException {

        // Fase 1
        tmpFotoFile = File.createTempFile("OCRPic", null);

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //Uri.fromFile(tmpFotoFile));
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == CAMERA_REQUEST) {
                Bitmap datifoto = null;
                try {
                        datifoto = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); <------ Here is the error
                } catch (FileNotFoundException e) {
                        throw new RuntimeException(e);
                } catch (IOException e) {
                        throw new RuntimeException(e);
                }
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                datifoto.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                bitmapdata = bos.toByteArray();
                //tmpFotoFile.delete();

                SimpleView view = new SimpleView(this, datifoto); // creo l'istanza
                                                                                                                        // // della                                                                                                       // view...
                setContentView(view); // e la setto

                Intent intentRecognize = new Intent(this, RecognitionActivity.class);
                intentRecognize.putExtra(ARRAY_BYTE, bitmapdata);
                intentRecognize.putExtra(GPS, gps);
                startActivity(intentRecognize);
        }

}
}


class SimpleView extends View{

private Bitmap bitmap;
private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

public SimpleView(Context context, Bitmap bitmap) {
        super(context);
        this.bitmap = Bitmap.createScaledBitmap(bitmap,480,320,false);//ridimensiono l'immagine
}

@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawBitmap( bitmap, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
}


}

而logcat是这样的:

02-05 16:41:50.321: E/AndroidRuntime(489): FATAL EXCEPTION: main
02-05 16:41:50.321: E/AndroidRuntime(489): java.lang.RuntimeException: Failure              delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/1285 (has extras) }} to activity {your.Mobile/your.Mobile.FotocameraActivity}: java.lang.RuntimeException: java.io.FileNotFoundException: Multiple items at content://media/external/images/media
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2984)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3027)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.app.ActivityThread.access$1100(ActivityThread.java:127)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1181)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.os.Looper.loop(Looper.java:137)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.app.ActivityThread.main(ActivityThread.java:4476)
02-05 16:41:50.321: E/AndroidRuntime(489):  at java.lang.reflect.Method.invokeNative(Native Method)
02-05 16:41:50.321: E/AndroidRuntime(489):  at java.lang.reflect.Method.invoke(Method.java:511)
02-05 16:41:50.321: E/AndroidRuntime(489):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
02-05 16:41:50.321: E/AndroidRuntime(489):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
02-05 16:41:50.321: E/AndroidRuntime(489):  at dalvik.system.NativeStart.main(Native Method)
02-05 16:41:50.321: E/AndroidRuntime(489): Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: Multiple items at content://media/external/images/media
02-05 16:41:50.321: E/AndroidRuntime(489):  at your.Mobile.FotocameraActivity.onActivityResult(FotocameraActivity.java:124)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.app.Activity.dispatchActivityResult(Activity.java:4820)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
02-05 16:41:50.321: E/AndroidRuntime(489):  ... 11 more
02-05 16:41:50.321: E/AndroidRuntime(489): Caused by: java.io.FileNotFoundException: Multiple items at content://media/external/images/media
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:612)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:628)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:557)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.content.ContentResolver.openInputStream(ContentResolver.java:392)
02-05 16:41:50.321: E/AndroidRuntime(489):  at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:725)
02-05 16:41:50.321: E/AndroidRuntime(489):  at your.Mobile.FotocameraActivity.onActivityResult(FotocameraActivity.java:122)
02-05 16:41:50.321: E/AndroidRuntime(489):  ... 13 more

希望你能帮我!(如果我不能很好地解释这个问题,我很抱歉!)

非常感谢!

塞尔吉奥

4

1 回答 1

6

使用 URI return in onActivityResultfromdata intent获取getBitmap

if (requestCode == CAMERA_REQUEST) {
      Bitmap datifoto = null;
      Uri picUri = data.getData();//<- get Uri here from data intent
       if(picUri !=null){
         try {
             datifoto = android.provider.MediaStore.Images.Media.getBitmap(
                                     this.getContentResolver(), 
                                     picUri);
         } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
    }
于 2013-02-05T16:12:47.960 回答