2013-02-06 32 views
1

我嘗試從相機拍攝照片,然後我嘗試通過fileProvider()方法在下面編寫的方法訪問在另一個活動中創建的這些two files,但對我來說很糟糕在接受點總是顯示我爲空。有人可以幫助我如何訪問在這裏創建的不同活動中的文件嗎?如何從另一個活動訪問文件

public class FileGenerator extends Activity { 
     File image1,image2,image3,image4; 
     private String imagePath1,imagePath2,imagePath3,imagePath4; 

     .................... 
     ......................... 

    public void cameraShot() // This method would be called when the user clicks on a button on my activity to make a request of taking pic. 
     { 

       ContentValues values = new ContentValues(); 
       Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

       //startActivityForResult(intentPicture,ACTION_TAKE_PICTURE); 

       Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

       if(flipvalue == 0) 
       { 
        intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, firstCapturedImageURI); 
        values.put(MediaStore.Images.Media.TITLE, "testingimage1.jpg"); 
        firstCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
        startActivityForResult(intentPicture,CAMERA_DATA_FIRST); 
       } 

       if(flipvalue == 1) 
       { 
        intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, secondCapturedImageURI); 
        values.put(MediaStore.Images.Media.TITLE, "testingimage2.jpg"); 
        secondCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
        startActivityForResult(intentPicture,CAMERA_DATA_SECOND); 


       } 
    } 

    protected void onActivityResult(int requestCode, int resultCode,Intent data) 
     { 
      super.onActivityResult(requestCode, resultCode, data); 

      if(resultCode==RESULT_OK) 
      { 
       Bundle extras= data.getExtras(); 

       if(requestCode == 0) 
       { 
        if(bitMap1 != null) 
        bitMap1.recycle(); 
        bitMap1 = (Bitmap)extras.get("data"); 
        imageView1.setImageBitmap(bitMap1); 

        selectedImagePath1 = getRealPathFromURI(firstCapturedImageURI); 

        imagePath1 = selectedImagePath1; 
        Log.d("Pic Path>>>", selectedImagePath1); 

        image1 = new File(selectedImagePath1); 

        flipvalue =1; 

       } 
          if (requestCode == 1) 
       { 
        if(bitMap2 != null) 
        bitMap2.recycle(); 
        bitMap2 = (Bitmap)extras.get("data"); 
        imageView2.setImageBitmap(bitMap2); 

        selectedImagePath2 = getRealPathFromURI(secondCapturedImageURI); 

        imagePath2 = selectedImagePath2; 
        Log.d("Pic Path>>>", selectedImagePath2); 
        image2 = new File(selectedImagePath2); 

        flipvalue =2; 

       } 
    } 

protected File fileProvider(int i) 
    { 
     if(i==1) 
     return image1; 
     else 
     return image2; 


    } 

public String getRealPathFromURI(Uri contentUri) 
    { 
     try 
     { 
      String[] proj = {MediaStore.Images.Media.DATA}; 
      Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
     catch (Exception e) 
     { 
      return contentUri.getPath(); 
     } 
    } 
} 

以下是我嘗試接收這些文件的活動。

public class FileReceiver extends Activity{ 
File receivedimage1, receivedimage2; 
FileGenerator filegen = new FileGenerator(); 
       .......................... 
        ............................ 

      receivedimage1 = filegen.fileProvider(1); 
      Log.d("File 1 ","file1 object"+receivedimage1);// always shows null 

      receivedimage2 = filegen.fileProvider(2); 
      Log.d("File 2 ","file2 object"+receivedimage2);// shows null 

} 

P.S. - (即,selectedImagePath1, selectedImagePath2)之前嘗試此,我常帶串到另一個活動,然後使用這些字符串創建文件。但不知道爲什麼這會破壞我創建的圖像文件。所以,我遵循上面的代碼方式,仍然沒有運氣。

+0

@ρяσѕρєяKI不能因爲我要通過的ArrayList是字符串的發送數據只鍵入,因爲我有另一個數據要作爲字符串發送。此外,我不能使用意圖,因爲它不是我必須從此活動開始的直接活動。你能以其他方式暗示我嗎? – rick

+0

@Alex_ios什麼是_不是直接activity_?如何在Android提供的概念上構建Android應用程序? ;) –

+0

@ClassStacker我的意思是接收活動不能從第一個活動通過意圖開始。而這是第一次活動和接收活動是第三次活動。即1→2→3。 – rick

回答

1

您需要使用Intent將文件路徑發送到其他活動,如果您有多於兩個活動,則使用SharedPreferences來從SharedPreferences中對其他活動中的文件路徑和回溯路徑進行存檔。

SharedPreferences pathPrefs = FileGenerator.this.getSharedPreferences(
              "filepathPrefs", MODE_WORLD_READABLE); 
     SharedPreferences.Editor prefsEditor = pathPrefs.edit(); 
     prefsEditor.putString("image1",image1.getAbsolutePath().toString()); 
     prefsEditor.putString("image2",image2.getAbsolutePath().toString()); 
     prefsEditor.commit(); 

和其他活動獲取文件路徑從SharedPreferences如:你可以在SharedPreferences作爲存儲路徑

SharedPreferences pathPrefs = 
        this.getSharedPreferences("filepathPrefs", MODE_WORLD_READABLE); 
String image1 = pathPrefs.getString("image1", "defaultpath"); 
String image2 = pathPrefs.getString("image2", "defaultpath"); 
// now use both file path in your code.. 
+0

+1,並感謝您的回覆。但是這會以字符串形式提取路徑。我怎樣才能使用這個字符串來獲取文件?因爲在接收活動中,我必須保持像新的FileBody(file1);所以,在這一行我需要給文件作爲參數。 – rick

+0

@Alex_ios:是隻傳遞字符串到'File file1 = new File(image1);新的FileBody(file1);'獲取文件 –

+0

實際上這是我的文件在發送時被破壞。所以,我在存儲列表中的路徑時也能正常工作。不知道爲什麼會發生:( – rick