0

ユーザーが写真を撮り、インテントを使用してこの画像をメールで送信できるアクティビティを作成しようと懸命に努力してきました。ファイル名を作成して文字列に保存しますが、最終的には値が取り除かれます。null にリセットされたときに、その値を保持する別の文字列変数を作成することになったので、その値を変更するためのアクセス権がないことを確認できましたが、2 番目の文字列も null として終了します。文字列の値を使用してインテントに渡し、撮影したばかりの画像を添付ファイルとして追加します。この変数の値が変更されている理由と方法を誰でも見ることができますか?

コメント付きの私のクラスは次のとおりです。

public class ImproveActivity extends Activity implements OnClickListener{
private static final String JPEG_FILE_PREFIX = "Improve_";
private static final String JPEG_FILE_SUFFIX = ".jpg";
private Bitmap mImageBitmap;
private ImageView mImageView;
private File storageDir;
private String mCurrentPhotoPath;
Button share, capture;
private String _path;
private File f;
//This variable holds file name
private String imageFileName;
//I am storing the file name variable here to keep its value
private String temp;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.improve_layout);
       mImageView = (ImageView)findViewById(R.id.imgview);
       capture = (Button)findViewById(R.id.capture);
       share = (Button)findViewById(R.id.share);

       capture.setOnClickListener(this);
       share.setOnClickListener(this);

}

private void dispatchTakePictureIntent() {

    if(isIntentAvailable(getApplicationContext(), MediaStore.ACTION_IMAGE_CAPTURE)){

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        ;
        try {
            //f is the image
            f = createImageFile();

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            startActivityForResult(takePictureIntent, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "error creating image", Toast.LENGTH_LONG).show();
        }

    }
    else{
        Toast.makeText(getApplicationContext(), "no camera available", Toast.LENGTH_LONG).show();
    }


}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent){
    Toast.makeText(getApplicationContext(), "returning", Toast.LENGTH_LONG).show();

    if (requestCode == 1) {

        mImageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
        if (mImageBitmap == null) {
            // bitmap still null
        } else {
            // set bitmap in imageview
            mImageView.setImageBitmap(mImageBitmap);
        }
    }

}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = 
        new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
   imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
   //display the current image file name
    Toast.makeText(getApplicationContext(), imageFileName, Toast.LENGTH_LONG).show();

    File file = new File(getAlbumDir().getAbsolutePath());
    file.mkdirs();              
    Toast.makeText(getApplicationContext(), imageFileName, Toast.LENGTH_LONG).show();
    File image = File.createTempFile(
        imageFileName, 
        JPEG_FILE_SUFFIX  
    );


    mCurrentPhotoPath = image.getAbsolutePath();
    temp = imageFileName;
    return image;

}

private File getAlbumDir() {
    return storageDir = new File(
            Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM
            ), 
            "Camera"
        );
}

public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}


@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.capture :
        dispatchTakePictureIntent();
        //displaying the value of the string imageFileName, it is what it should be, the file name
        Toast.makeText(getApplicationContext(), imageFileName, Toast.LENGTH_LONG).show();

        //displaying the file name in temp at this point it is still the file name I need
        Toast.makeText(getApplicationContext(), "temp is " +temp, Toast.LENGTH_LONG).show();
        break;
        //Once the image is saved, clicking share to open chooser
    case R.id.share:

        //displaying the temp string the file name is gone it is now null ????
        Toast.makeText(getApplicationContext(), "temp is " +temp, Toast.LENGTH_LONG).show();

        Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);  
        picMessageIntent.setType("image/jpg");  
        File downloadedPic =  new File(  
            Environment.getExternalStoragePublicDirectory(  
            Environment.DIRECTORY_DCIM),  
            "Camera/"+ temp+"jpg");  //I want to use the file name string to send the image via email
        picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));  
        startActivity(Intent.createChooser(picMessageIntent, "Send your picture using:"));  
        break;
    }
}

誰かがこれがどのように起こっているかについて光を当てることができれば、私は感謝しています.

Here is the logcat, I open the activity, take a photo, save it, then it goes back to the activity view, I press share and the toast displays the value of temp while the chooser opens.

11-25 17:56:34.573: D/dalvikvm(12037): GC_EXTERNAL_ALLOC freed 1176 objects / 87312 bytes in 53ms
11-25 17:56:39.313: W/IInputConnectionWrapper(12037): showStatusIcon on inactive InputConnection
11-25 17:56:49.803: W/System.err(12037): java.lang.NullPointerException: Argument must not be null
11-25 17:56:49.808: W/System.err(12037):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
11-25 17:56:49.808: W/System.err(12037):    at java.io.FileInputStream.<init>(FileInputStream.java:134)
11-25 17:56:49.808: W/System.err(12037):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:349)
11-25 17:56:49.808: W/System.err(12037):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:399)
11-25 17:56:49.808: W/System.err(12037):    at com.seweb.app.ImproveActivity.onActivityResult(ImproveActivity.java:94)
11-25 17:56:49.808: W/System.err(12037):    at android.app.Activity.dispatchActivityResult(Activity.java:3890)
11-25 17:56:49.808: W/System.err(12037):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
11-25 17:56:49.808: W/System.err(12037):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3115)
11-25 17:56:49.808: W/System.err(12037):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
11-25 17:56:49.813: W/System.err(12037):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
11-25 17:56:49.813: W/System.err(12037):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
11-25 17:56:49.813: W/System.err(12037):    at android.app.ActivityThread.access$2400(ActivityThread.java:125)
11-25 17:56:49.813: W/System.err(12037):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
11-25 17:56:49.818: W/System.err(12037):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 17:56:49.818: W/System.err(12037):    at android.os.Looper.loop(Looper.java:123)
11-25 17:56:49.818: W/System.err(12037):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-25 17:56:49.818: W/System.err(12037):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 17:56:49.818: W/System.err(12037):    at java.lang.reflect.Method.invoke(Method.java:521)
11-25 17:56:49.823: W/System.err(12037):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-25 17:56:49.823: W/System.err(12037):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-25 17:56:49.823: W/System.err(12037):    at dalvik.system.NativeStart.main(Native Method)
11-25 17:56:50.273: D/dalvikvm(12037): GC_EXTERNAL_ALLOC freed 1920 objects / 108152 bytes in 136ms
4

4 に答える 4

4

Strings は、SharedPreferences、データベース、または汎用ファイルに永続的に保存する必要があります。

組み込みのカメラ アプリで写真を撮るためにアクティビティを終了すると、アプリはバックグラウンドに入ります。追加のメモリが必要な場合、バックグラウンドのアプリは OS によって強制終了されるリスクがあります。これが発生すると、すべての変数がデフォルトの null 値にリセットされます...


追加
LogCat から:

java.lang.NullPointerException: Argument must not be null
    ...
    at com.seweb.app.ImproveActivity.onActivityResult(ImproveActivity.java:94)

94行目は次のようになっていると思います:

mImageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

これは、アプリがバックグラウンドで破棄されて再起動された場合に意味があります。このようにアプリが完全に再起動すると、のみ onCreate()が呼び出され、createImageFile()その他のメソッドは呼び出されません。

mCurrentPhotoPathSharedPreferences にも保存し、 nullの場合はいつでもimageFileNameこれらの値を復元します。onActivityResult()mCurrentPhotoPath


SharedPreferences の使用
これらのメソッドをアクティビティに追加します。

private void savePreferences(){
    // We need an Editor object to make preference changes.
    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    editor.putString("imageFileName", imageFileName);
    editor.putString("mCurrentPhotoPath", mCurrentPhotoPath);

    // Commit the edits!
    editor.commit();
}

private void restorePreferences() {
    SharedPreferences settings = getPreferences(MODE_PRIVATE);
    imageFileName = settings.getString("imageFileName", "");
    mCurrentPhotoPath = settings.getString("mCurrentPhotoPath", "");
}

あなたが言ったように、あなたがデバッグしている間tempの単なる複製であると私は無視しました。 次に、これらのメソッドを適切なタイミングで呼び出す必要があります。まず、Activity を離れて写真を撮る直前に、両方の文字列を保存します。imageFileName

try {
    savePreferences();
    ...
    startActivityForResult(takePictureIntent, 1);
} 

次に、これらの値を でリロードする必要があるかどうかを確認しますonActivityResult()

if (requestCode == 1) {
    if(mCurrentPhotoPath == null)
        restorePreferences();

    mImageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
    ...
于 2012-11-25T17:28:04.570 に答える
0

変数を初期化してみてください:

String temp = "";
于 2012-11-25T17:22:30.337 に答える
0

はい、変数を初期化して NullPointerException を取得しないようにしますが、問題は、なぜこの時点で null なのかということです。null 文字列を取得した場合に写真が撮影されることは確かですか? スタックトレースを表示してください。CreateImageFile メソッドで何か問題が発生している可能性があります。このメソッドは例外をスローすることがありますか? はいの場合、この文字列は初期化できませんでした。

于 2012-11-25T17:29:17.697 に答える