2017-08-13 83 views
-1

当我通过相机捕获图像时,此代码中出现错误。我想将缩略图存储在设备存储的MyImages文件夹中。我得到错误我想创建从相机捕获的图像的缩略图

引起:java.lang.NullPointerException:file == null“at”fos = new FileOutputStream(thumbnailFile);

在forActivityResult代码

public class FragmentTwo extends Fragment { 
Button btn1, btn2; 
View v; 
File image; 
private static final int REQUEST_IMAGE_CAPTURE = 1; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    v = inflater.inflate(R.layout.fragment_two, container, false); 
    btn1 = (Button) v.findViewById(R.id.btn1); 
    btn2 = (Button) v.findViewById(R.id.btn2); 

    btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 


      File file = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
      file.mkdirs(); 

      File image = new File(file, "picture1" + ".jpg"); 
      Uri uriSavedImage = Uri.fromFile(image); 
      i.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
      startActivityForResult(i, REQUEST_IMAGE_CAPTURE); 
     } 
    }); 


    btn2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
      imagesFolder.mkdirs(); 

      File image = new File(imagesFolder, "picture2" + ".jpg"); 
      Uri uriSavedImage = Uri.fromFile(image); 

      i.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 

      startActivity(i); 
     } 
    }); 
    return v; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    final int THUMBSIZE = 64; 
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile("/MyImages/picture1"), 
     THUMBSIZE, THUMBSIZE); 

File thumbnailFile = image; 
    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(thumbnailFile); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
    try { 
     fos.flush(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

} 

回答

0

您的告诉摄像机应用,在那里保存图像创建File代码如下所示:

 File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
     imagesFolder.mkdirs(); 

     File image = new File(imagesFolder, "picture2" + ".jpg"); 

你创建的代码用于解码图像的不太完全的File看起来像这样:

 "/MyImages/picture1" 

这些都不是相同的:

  • 第一有Environment.getExternalStorageDirectory(),第二不

  • 第一具有.jpg延伸,并且所述第二不

因此,BitmapFactory找不到此文件。

此外,你不检查看到你实际上在onActivityResult()得到了积极的结果。如果用户决定不拍照并按BACK退出相机活动,则不会有照片。

此外,这是不行的:

File thumbnailFile = image; 

你永远不分配一个值image领域,所以imagenullthumbnailFilenull,当你尝试创建FileOutputStream你会崩溃。要解决此问题,请在File image = new File(file, "picture1" + ".jpg");的位置替换为image = new File(file, "picture1" + ".jpg");,以便不要创建名为image的本地变量,而是将该值分配给名为image的字段。

您将面临进一步的问题(例如,不能在Android 7.0+上使用Uri.fromFile(),不处理在摄像头应用处于前台时终止进程的情况)。

+0

我该如何纠正? @CommonsWare – Abhi