【问题标题】:move image from one folder to another in android sdcard在android sdcard中将图像从一个文件夹移动到另一个文件夹
【发布时间】:2015-04-12 07:37:08
【问题描述】:

在我的项目中,我从图库中选择图像,并希望将其从图库移动到我 sdcard 中的其他文件夹....到目前为止,我已经完成了图像的选择,并且图像的路径也得到了。 ..现在我必须从该文件夹中移动该图像.... 提前致谢

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;
    }
}

【问题讨论】:

  • 是的。好的。问题是什么?在你说出哪里出错之前,我当然没有看你的代码。

标签: android file android-sdcard


【解决方案1】:

确保您具有写入文件的权限。你可以这样做:

<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();
        }
    }
}

【讨论】:

  • if(!destFile.exists()) { destFile.createNewFile(); }。请删除它。它不需要,因为新的 FileOutputStream 会照顾它。
  • 即使目标文件无法创建或未完全复制,您也在删除源文件。用户不会喜欢这样的。
  • 不知何故我已将文件移动到我的位置,但是当我从图库中移动图像时,它也显示了文件...我不希望该 img 显示在图库中..我想删除它@亚历克萨努斯
  • @Alexanus file.delete() 不起作用,我也尝试了 file.deleteOnExit() ...图库仍然显示该文件
猜你喜欢
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
相关资源
最近更新 更多