2016-05-18 35 views
0

how to put a picture in the internal memory of the app? 這是我的問題,它涉及在應用程序中放置圖片,我希望更具體。 我需要的是,該庫將無法讓我查看的圖片已保存在設備在畫廊無法訪問的地方保存圖片

這裏後的代碼:

public class Add_Comment_Picture extends AppCompatActivity { 
    static final int CAM_REQUEST = 1; 
    ImageView imageView; 
    ImageButton button; 
    private static final int REQUEST_CODE = 1; 
    String mCurrentPhotoPath; 
    static final int REQUEST_TAKE_PHOTO = 1; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_add__comment__picture); 
    // ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE); 
     button = (ImageButton) findViewById(R.id.imageButtonCamera); 
     imageView = (ImageView) findViewById(R.id.imageView); 

     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       File file = getFile(); 
       camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
       startActivityForResult(camera_intent, CAM_REQUEST); 
      } 
     }); 


    } 

    private String createFileName(){ 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     return "JPEG_" + timeStamp + "_.jpg"; 
    } 



    @Override 
    protected void onStart() { 
     super.onStart(); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

      int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); 
      int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE); 
      int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA); 

      List<String> permissions = new ArrayList<String>(); 
      if (hasWritePermission != PackageManager.PERMISSION_GRANTED) { 
       permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); 
      } 

      if (hasReadPermission != PackageManager.PERMISSION_GRANTED) { 
       permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE); 
      } 

      if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) { 
       permissions.add(Manifest.permission.CAMERA); 
      } 
      if (!permissions.isEmpty()) { 
       requestPermissions(permissions.toArray(new String[permissions.size()]), 111); 
      } 
     } 


    } 


    private File getFile() 
    { 
     //File folder = new File("sdcard/camera_app"); 
     File folder = new File("sdcard/data/data/com.yuvaleliav1gmail.foodchain/photos"); 
     if (!folder.exists()) 
     { 
      folder.mkdir(); 
     } 
     mCurrentPhotoPath = createFileName(); 
     File image_file = new File(folder,mCurrentPhotoPath); 
     mCurrentPhotoPath = "sdcard/data/data/com.yuvaleliav1gmail.foodchain/photos/" + mCurrentPhotoPath; 
     return image_file; 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // String path = mCurrentPhotoPath; 
     imageView.setImageDrawable(Drawable.createFromPath(mCurrentPhotoPath)); 
     super.onActivityResult(requestCode, resultCode, data); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
      case 111: { 
       for (int i = 0; i < permissions.length; i++) { 
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]); 


        } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) { 
         System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]); 

        } 
       } 
      } 
      break; 
      default: { 
       super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
      } 
     } 
    } 
} 
+0

@vrund purohit:拼寫錯誤的文件擴展名,請修改爲「.nomedia」 –

+1

只需將文件放入應用程序的內部存儲器即可。 Galery應用程序無法訪問您的應用程序內部存儲器。 – greenapps

+0

greenapps,這正是我想要做的......我該怎麼做?!?! –

回答

0

你有兩個選擇:

  1. 將與在開始點文件夾中的文件,例如.myimages

  2. 創建一個空文件c alled .nomedia文件夾內與圖片

0

只需創建與同點開始,例如名稱的文件夾:AppName的/ .Images。現在將圖像保存到該文件夾​​中,圖庫將無法訪問文件夾AppName/.Images中的圖像。