Android 字符串变量最终为空,我不知道为什么

Android 字符串变量最终为空,我不知道为什么,android,Android,我一直在努力创建一个活动,允许用户使用意图拍照并通过电子邮件发送此图像。我创建了文件名并将其存储在一个字符串中,但最终它的值被删除了。当它被重置为null时,我创建了另一个字符串变量来保存它的值,这样我就可以确保没有任何东西可以更改它的值,但是第二个字符串也会以null结束。我使用字符串的值并将其传递给intent,以便将刚刚拍摄的图像添加为附件。有人知道这个变量的值为什么会改变,以及如何改变吗 这是我的课堂,带着评论: public class ImproveActivity extends

我一直在努力创建一个活动,允许用户使用意图拍照并通过电子邮件发送此图像。我创建了文件名并将其存储在一个字符串中,但最终它的值被删除了。当它被重置为null时,我创建了另一个字符串变量来保存它的值,这样我就可以确保没有任何东西可以更改它的值,但是第二个字符串也会以null结束。我使用字符串的值并将其传递给intent,以便将刚刚拍摄的图像添加为附件。有人知道这个变量的值为什么会改变,以及如何改变吗

这是我的课堂,带着评论:

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;
    }
}
公共类ImproveActivity扩展活动实现OnClickListener{
私有静态最终字符串JPEG\u文件\u PREFIX=“改进”;
私有静态最终字符串JPEG_FILE_SUFFIX=“.jpg”;
私有位图;
私有图像视图mImageView;
私有文件存储目录;
私有字符串mCurrentPhotoPath;
按钮共享、捕获;
私有字符串路径;
私有文件f;
//此变量保存文件名
私有字符串文件名;
//我将文件名变量存储在这里以保持其值
私有字符串温度;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.改进_布局);
mImageView=(ImageView)findViewById(R.id.imgview);
capture=(按钮)findviewbyd(R.id.capture);
share=(按钮)findViewById(R.id.share);
capture.setOnClickListener(this);
share.setOnClickListener(this);
}
私有无效DispatchTakePictureContent(){
如果(isIntentAvailable(getApplicationContext(),MediaStore.ACTION\u IMAGE\u CAPTURE)){
Intent takePictureIntent=新的意图(MediaStore.ACTION\u IMAGE\u CAPTURE);
;
试一试{
//f是图像
f=createImageFile();
takePictureContent.putExtra(MediaStore.EXTRA_输出,Uri.fromFile(f));
startActivityForResult(图片内容1);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
Toast.makeText(getApplicationContext(),“创建图像时出错”,Toast.LENGTH_LONG.show();
}
}
否则{
Toast.makeText(getApplicationContext(),“没有可用的摄像头”,Toast.LENGTH_LONG.show();
}
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图){
Toast.makeText(getApplicationContext(),“returning”,Toast.LENGTH_LONG.show();
if(requestCode==1){
mImageBitmap=BitmapFactory.decodeFile(mCurrentPhotoPath);
if(mImageBitmap==null){
//位图仍然为空
}否则{
//在imageview中设置位图
setImageBitmap(mImageBitmap);
}
}
}
私有文件createImageFile()引发IOException{
//创建图像文件名
字符串时间戳=
新的SimpleDataFormat(“yyyyMMdd_HHmmss”).格式(新日期());
imageFileName=JPEG\u文件\u前缀+时间戳+“\u”;
//显示当前图像文件名
Toast.makeText(getApplicationContext(),imageFileName,Toast.LENGTH_LONG).show();
File File=新文件(getAlbumDir().getAbsolutePath());
mkdirs()文件;
Toast.makeText(getApplicationContext(),imageFileName,Toast.LENGTH_LONG).show();
File image=File.createTempFile(
imageFileName,
JPEG\u文件\u后缀
);
mCurrentPhotoPath=image.getAbsolutePath();
temp=imageFileName;
返回图像;
}
私有文件getAlbumDir(){
return storageDir=新文件(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY\u DCIM
), 
“照相机”
);
}
公共静态布尔值isIntentAvailable(上下文、字符串操作){
final-PackageManager-PackageManager=context.getPackageManager();
最终意图=新意图(行动);
列表=
packageManager.QueryInputActivities(仅适用于意图、packageManager.MATCH_默认值);
返回列表.size()>0;
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.capture:
DispatchTakePictureContent();
//显示字符串imageFileName的值,它应该是什么,文件名
Toast.makeText(getApplicationContext(),imageFileName,Toast.LENGTH_LONG).show();
//此时在temp中显示文件名仍然是我需要的文件名
Toast.makeText(getApplicationContext(),“temp is”+temp,Toast.LENGTH\u LONG.show();
打破
//保存图像后,单击“共享”打开选择器
案例R.id.share:
//显示临时字符串文件名消失,现在为空????
Toast.makeText(getApplicationContext(),“temp is”+temp,Toast.LENGTH\u LONG.show();
Intent picMessageIntent=newintent(android.content.Intent.ACTION\u SEND);
picMessageIntent.setType(“image/jpg”);
文件下载PIC=新文件(
环境。getExternalStoragePublicDirectory(
环境。目录(DCIM),
“Camera/”+temp+“jpg”);//我想使用文件名字符串通过电子邮件发送图像
picMessageIntent.putExtra(Intent.EXTRA_流,Uri.fromFile(downloadedPic));
startActivity(Intent.createChooser(picMessageIntent,“使用以下方式发送图片”);
打破
}
}
如果有人能告诉我这是怎么发生的,我将不胜感激

这是logcat,我打开活动,拍照,保存,然后它返回到活动视图,我按share,toast在选择器打开时显示temp的值

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
11-2517:56:34.573:D/dalvikvm(12037):GC_EXTERNAL_ALLOC在53ms内释放了1176个对象/87312个字节
11-25 17:56:39.313:W/IIInputConnectionWrapper(12037):在非活动输入连接上显示状态图标
11-2517:56:49.803:W/System.e
String temp = "";
java.lang.NullPointerException: Argument must not be null
    ...
    at com.seweb.app.ImproveActivity.onActivityResult(ImproveActivity.java:94)
mImageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
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", "");
}
try {
    savePreferences();
    ...
    startActivityForResult(takePictureIntent, 1);
} 
if (requestCode == 1) {
    if(mCurrentPhotoPath == null)
        restorePreferences();

    mImageBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
    ...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putAll(getIntent().getExtras());
    savedInstanceState.putString("currentPhotoPath", mCurrentPhotoPath);
    super.onSaveInstanceState(savedInstanceState);
}
        if (savedInstanceState != null) {
mCurrentPhotoPath = savedInstanceState.getString("currentPhotoPath", mCurrentPhotoPath);
    }