在我的项目中,我正在从图库中选择图像,并希望将其从图库移动到我的SD卡中的其他文件夹....到目前为止,我已经选择了图像,并且还获得了图像的路径。 ..现在我必须从该文件夹中移动该图像.... 提前谢谢
MainActivity
public class MainActivity extends ActionBarActivity {
ImageView iv;
Button load,cp;
TextView tv,tvpath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.imageView);
tv = (TextView)findViewById(R.id.textView);
tvpath = (TextView)findViewById(R.id.textView2);
load = (Button)findViewById(R.id.button);
cp = (Button)findViewById(R.id.button2);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
startActivityForResult(i, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode== 1 && resultCode == RESULT_OK && data != null){
String realpath;
if (Build.VERSION.SDK_INT < 11){
realpath = RealPathUtil.getRealPathFromURI_BelowAPI11(this,data.getData());
}else if (Build.VERSION.SDK_INT < 19){
realpath = RealPathUtil.getRealPathFromURI_BelowAPI11to18(this, data.getData());
}else{
realpath = RealPathUtil.getRealPathFromURI_API19(this, data.getData());
}
settext(Build.VERSION.SDK_INT,data.getData().getPath(),realpath);
changepath(realpath);
/* Uri pickimage = data.getData();
String[] filepath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickimage,filepath,null,null,null);
cursor.moveToFirst();
String imgpath = cursor.getString(cursor.getColumnIndex(filepath[0]));
iv.setImageBitmap(BitmapFactory.decodeFile(imgpath));
cursor.close();
Toast.makeText(getApplicationContext(),filepath[0],Toast.LENGTH_SHORT).show();
*/
}
}
//tried to chage the path here
private void changepath(String realpath) {
File rp = new File(realpath);
File cp = new File("/mnt/sdcard/myapp/");
rp.renameTo(cp);
String s = rp.getAbsolutePath();
Toast.makeText(getApplicationContext(),"new path is:"+s,Toast.LENGTH_LONG).show();
}
private void settext(int sdkInt, String path, String realpath) {
this.tv.setText("uri path :"+path);
this.tvpath.setText("Real path :"+realpath);
Uri uri = Uri.fromFile(new File(realpath));
iv.setImageURI(uri);
}
}
**RealPathUtil**
public class RealPathUtil {
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public static String getRealPathFromURI_BelowAPI11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_API19(Context context, Uri data) {
String filepath = "";
String wholeID = DocumentsContract.getDocumentId(data);
String[] column = {MediaStore.Images.Media.DATA};
String sel = MediaStore.Images.Media._ID + "=?";
String id = wholeID.split(":")[1];
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,column,sel,new String[]{ id },null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()){
filepath = cursor.getString(columnIndex);
}
cursor.close();
return filepath;
}
}
答案 0 :(得分:0)
确保您具有写入文件的权限。你这样做:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
在你的AndroidManifest.xml
中如果您移动位图的方法失败,可以尝试以下方法:
path_source
是位图的路径,path_destination
是新路径。 (你想要移动位图的地方)
public static void MoveFile(String path_source, String path_destination) throws IOException {
File file_Source = new File(path_source);
File file_Destination = new File(path_destination);
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(file_Source).getChannel();
destination = new FileOutputStream(file_Destination).getChannel();
long count = 0;
long size = source.size();
while((count += destination.transferFrom(source, count, size-count))<size);
file_Source.delete();
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}