这是使用图库中的“发送到/共享菜单”将图像复制到存储中预定义的隐藏文件夹并从用户图库中删除文件的工作代码
SendToActivity.java
package com.company.privategallery;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class SendToActivity extends Activity {
private static final int SELECT_PICTURE = 0;
//Generating Random Number to use as a unique file name
static int random = (int)Math.ceil(Math.random()*100000000);
private static String fname = Integer.toString(random);
private static String selectedImagePath;
//Getting the external Path to the Storage
private static String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();
//Setting a directory that is already created on the Storage to copy the file to.
private static String targetPath = ExternalStorageDirectoryPath + "/DCIM/privgal/.nomedia/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get the received intent
Intent receivedIntent = getIntent();
//get the action
String receivedType = receivedIntent.getType();
//make sure it's an action and type we can handle
if(receivedType.startsWith("image/")){
//get the uri of the received image
Uri receivedUri = (Uri)receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
selectedImagePath = getPath(receivedUri);
System.out.println("Image Path : " + selectedImagePath);
//check we have a uri
if (receivedUri != null) {
//Copy the picture
try {
copyFile(selectedImagePath, targetPath + fname + ".jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startGallery();
deleteFile();
onDestroy();
}
}
}
public void copyFile(String selectedImagePath, String string) throws IOException {
InputStream in = new FileInputStream(selectedImagePath);
OutputStream out = new FileOutputStream(string);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Toast customToast = new Toast(getBaseContext());
customToast = Toast.makeText(getBaseContext(), "Image Transferred", Toast.LENGTH_LONG);
customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
customToast.show();
}
private void startGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, SELECT_PICTURE);
}
// Delete the file that was copied over
private void deleteFile() {
File fileToDelete = new File(selectedImagePath);
boolean fileDeleted = fileToDelete.delete();
// request scan
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(fileToDelete));
sendBroadcast(scanIntent);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
finish();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
@Override
public void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
finish();
}
}
在 Manafest 中添加了活动和权限
权限:
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"> </uses-permission>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"> </uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
活动:
<activity android:name="com.company.privategallery.SendToActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>