将图片保存在画廊无法访问的地方
saving a picture in a place where the gallery will not be able to access it
这是我的问题,它涉及在应用程序中放置图片,我想更具体一些。
我需要的是图片保存到设备后图库不能让我查看图片
代码如下:
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);
}
}
}
}
你有两个选择:
将文件放在以点开头的文件夹中,例如.myimages
在包含图片的文件夹中创建一个名为 .nomedia
的空文件
只需创建一个名称以点开头的文件夹,例如:AppName/.Images。现在将图像保存到该文件夹中,图库将无法访问文件夹 AppName/.Images.
中的图像
代码如下:
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);
}
}
}
}
你有两个选择:
将文件放在以点开头的文件夹中,例如
.myimages
在包含图片的文件夹中创建一个名为
.nomedia
的空文件
只需创建一个名称以点开头的文件夹,例如:AppName/.Images。现在将图像保存到该文件夹中,图库将无法访问文件夹 AppName/.Images.
中的图像