2017-04-17 105 views
0

我正在构建一个简单的相机应用程序在Android中显示它在imageView。我可以得到非常模糊的缩略图。所以我有一个制作的文件,然后它给了我的文件的位置。当我检查它时,它是0 kb,这是很好理解。 我的任务是捕获图像并保存在mCurrentPhotoPath,我应该能够在我imageViewstartActivityForResult()如何在位置获取图像并在Android应用程序中检索imageView?

显示它不过应用崩溃连连Caling在此之前之前。 我知道的东西是越野车在这条线

Uri photoURI = FileProvider.getUriForFile(MainActivity.this,"com.infolabs.photu", photoFile); 

。我已经改变了Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider",photoFile);com.infolabs.photu。但我不能让什么是错的吧。
请看看我的代码,我有位置URL的位置,但它不会显示或工作的形象是0KB

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    b = (Button) findViewById(R.id.captureimage); 
    imageView = (ImageView) findViewById(R.id.imageView); 


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

      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 


      if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
      { 
       // Create the File where the photo should go 
       File photoFile = null; 
       try 
       { 
        photoFile = createImageFile(); 
        Toast.makeText(MainActivity.this,mCurrentPhotoPath,Toast.LENGTH_LONG).show(); 
       } 
       catch (IOException ex) 
       { 
        Toast.makeText(MainActivity.this,"the file is not created ",Toast.LENGTH_SHORT).show(); 
       } 

       if (photoFile != null) 
       { 
        //this Uri does not working properly 
        Uri photoURI = FileProvider.getUriForFile(MainActivity.this,"com.infolabs.photu", photoFile); 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
       } 
      } 
     } 
    }); 
} 

错误日志: - 致命异常:主要 过程:infolabs PID:21415 java.lang.NullPointerException:试图调用虚方法android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String) '在android.support.v4.content.FileProvider.parsePathStrategy上的空对象引用 (FileProvider.java:58 3) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399) at infolabs.photu.MainActivity $ 1 .onClick(MainActivity.java:71) at android.view.View.performClick(View.java:5269) at android.view.View $ PerformClick.run(View.java:21556) at android.os.Handler .handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main (ActivityThread.java:5776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679) 04-17 16:56:15.085 21415-21425/infolabs.photu I /系统:FinalizerDaemon:最终确定的对象= 65

+0

如果它崩溃,那么也发布你的崩溃日志。 –

+0

您可以在“Uri photoURI = FileProvider.getUriForFile(MainActivity.this,”com.infolabs.photu“,photoFile)中添加调试点;”并手动打开文件并检查文件的大小。 – Sarker

+0

是的,我没有显示0kb –

回答

0

或者,你可以尝试毕加索库,这是很容易并将解决您在处理图像方面的大部分问题。 你可以在这里找到:http://square.github.io/picasso/

+0

先生,我的问题是首先捕捉图像并妥善保存,然后我可以从sdcard加载图像。 –

+0

然后@VivekGautam,如果你可以发布崩溃日志,那么找出问题会很有帮助。 –

1

捕捉来自摄像头的图像,并将其保存到SD卡:

cameraBtn.setOnClickListener(new View.OnClickListener() 
{ 
          @Override 
          public void onClick(View v) 
{ 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

        try { 

         File f = createImageFile();//createImageFile() is added. 
         if (f != null) { 
          intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
          startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
         } 
        } catch (IOException IOE) { 
         IOE.printStackTrace(); 
        } 

          } 
         }); 


     private File createImageFile() throws IOException { 

       String imageFileName = "image"; 
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
       imageFileName = imageFileName + timeStamp.toString(); 
       File albumF = getStorageDir(); 
       File imageF = File.createTempFile(imageFileName, ".jpg", albumF); 
       return imageF; 

      } 

     private File getStorageDir() { 
       File storageDir = null; 
       storageDir = new File(Environment.getExternalStorageDirectory(), "/MyApp"); 
       if (storageDir != null) { 
        if (!storageDir.mkdirs()) { 
         if (!storageDir.exists()) { 
          Log.d("CameraSample", "failed to create directory"); 
          return null; 
         } 
        } 
       } 
       return storageDir; 
      } 



     private File getImageFile() { 
      String Path = Environment.getExternalStorageDirectory() + "/MyApp"; 
      File f = new File(Path); 
      File imageFiles[] = f.listFiles(); 

      if (imageFiles == null || imageFiles.length == 0) { 
       return null; 
      } 

      File lastModifiedFile = imageFiles[0]; 
      for (int i = 1; i < imageFiles.length; i++) { 
       if (lastModifiedFile.lastModified() < imageFiles[i].lastModified()) { 
        lastModifiedFile = imageFiles[i]; 
       } 
      } 
      return lastModifiedFile; 
     } 

//覆盖从活动的方法,读取保存图像文件,并显示在ImageView的:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 

       File imageFile = getImageFile(); 

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 
        mImageView.setImageBitmap(bitmap); 

}}} 

添加权限在menifest文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.CAMERA" /> 
相关问题