2011-04-23 85 views
1

我现在正在启动相机并拍摄照片的应用程序,我没有使用相机活动,但我写了相机应用程序,我想将拍摄的图像保存在内部电话存储文件夹名为“temPic”在android内部存储器中存储图像

下面的代码生成文件夹和图像,但是当我检查tempPic时,我发现了一个名为image1.jpg的图像,它的大小是461115(我试图将图像存储在SDcard目录和它是相同的大小),但当我双击它出现一个黑色的图像,而不是采取的一个虽然在SD卡我打开它!

FileManipulator fileFormat = new FileManipulator(
       getApplicationContext()); 
     String path = fileFormat.createFolder_PStrg("tempPic") + "/image1.jpg";  
     File file = new File(path); 
     Uri outputFileUri = Uri.fromFile(file); 

     OutputStream imageFileOS; 
     try { 


      imageFileOS = getContentResolver().openOutputStream(outputFileUri); 
      imageFileOS.write(arg0); 
      imageFileOS.flush(); 
      imageFileOS.close(); 
      Toast.makeText(AndroidCamera.this, 
        file.length()+"", 
        Toast.LENGTH_LONG).show(); 
      Toast.makeText(AndroidCamera.this, 
        "Image saved: " + outputFileUri.toString(), 
        Toast.LENGTH_LONG).show(); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     camera.startPreview(); 
+0

FILEFORMAT。 createFol der_PStrg是一种在内部存储器中创建文件夹的方法 – alex 2011-04-23 20:06:04

回答

8

我不知道这是否可以帮助你,但这是我如何将文件保存到SD卡,它的工作原理。

 public void saveToSD(Bitmap outputImage){ 


      File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/"); 
      storagePath.mkdirs(); 

      File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg"); 

      try { 
       FileOutputStream out = new FileOutputStream(myImage); 
       outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 
       out.flush();  
       out.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      }    
     } 
+0

它可以在内部存储上运行吗? – Behzad 2013-06-05 05:20:14

+1

这种情况下的内部意味着内置于设备中的SD卡,但它不是以不同方式访问的应用程序专用存储。 – Lumis 2013-06-05 15:16:19

0

你为什么不使用摄像头的活动,并通过创建一个具有写入权限,允许其他应用程序写入到这个文件夹,并把图像的路径,然后之后的临时文件夹保存图像的内部存储相机活动回报您可以将图像移动到另一个内部存储文件夹,但与私有权限如下



//the temp folder to start the camera activity with 
String path = getDir("images", Context.MODE_WORLD_WRITEABLE).getPath() + "/test.jpg"; 

//start the camera activity 
File file = new File(path); 
Uri outputFileUri = Uri.fromFile(file); 

Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
startActivityForResult(intent, CAMERA_ACTIVITY); 

,并在onActivityResult方法将它与私人权限文件夹它会正常工作,因为我测试它

+0

这不工作我真正的设备上 – 2015-02-24 09:08:07

相关问题