2017-04-11 56 views
0

我尝试创建一个应用程序,可以从相机捕获图像并根据图像路径显示结果。内,其处理捕获图像,如下捕获图像后的更新库android

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

       file = new File(Environment.getExternalStorageDirectory() + File.separator + "Nama_foto_" + waktu + ".jpg"); 

       intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
       intent.putExtra("return-data", true); 

       getParentFragment().startActivityForResult(intent, CAPTURE_IMAGE); 
      }catch (ActivityNotFoundException anfe){ 
       //display an error message 
       String errorMessage = "Whoops - your device doesn't support capturing images!"; 
       Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT); 
       toast.show(); 
      } 
     } 
    }); 

而作为从相机捕捉下面代码

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if(requestCode == CAPTURE_IMAGE) { 

      performCrop(); 
     } 
     else if(requestCode == PIC_CROP){ 
      //get the returned data 
      Bundle extras = data.getExtras(); 
      //get the cropped bitmap 
      Bitmap thePic = extras.getParcelable("data"); 

      picturePath = file.getAbsolutePath(); 

      try { 
       ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
       thePic.compress(Bitmap.CompressFormat.JPEG, 80, bytes); 

       file.createNewFile(); 
       FileOutputStream out = new FileOutputStream(file); 
       out.write(bytes.toByteArray()); 
       out.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      //retrieve a reference to the ImageView 
      //ImageView picView = (ImageView)findViewById(R.id.picture); 
      //display the returned cropped image 
     imgView.setImageBitmap(thePic); 
     } 

     try { 

     }catch (Exception e) { 
      Toast.makeText(getActivity(), "Maaf gagal mengambil foto.", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
} 

}

声明OnCreateView外部作为一个空字符串变量picturePath林书面respon的按钮。 问题是,每当我运行应用程序并捕获图像。图像没有立即显示在画廊中,而是我需要重新运行应用程序(通过android studio)在图库中查看图像。看起来像oncreateview内的东西触发事件。但我仍然无法弄清楚什么。

请帮我解决这个问题。由于

回答

1

似乎是你的问题应该区别改写: 这可能会解决你的问题

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile))); 

参考: How can I update the Android Gallery after a photo?

+0

我改变了我的问题,,,我应该在哪里申报上面的代码? – BrenDonie

+0

试过我们的代码stil没有显示/保存在Android图库。但在我的应用程序中显示图像 – BrenDonie

+0

哦,把它放在onActivityResult。好的工作....非常感谢你:) – BrenDonie