5

我通过以下代码从图库中获取图像:

接受 其他答案解释了如何发送意图,但他们没有很好地解释如何处理响应。这是有关如何执行此操作的一些示例代码:

protected void onActivityResult(int requestCode, int resultCode, 
       Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(
                               selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        }
    }
}

现在,我想从文件路径中获取文件名,重命名并再次保存。这可能吗?如果是,如何?

4

1 回答 1

5

它总是可能的!

//get file
File photo = new File(filePath);

//file name
String fileName = photo.getName();

//resave file with new name
File newFile = new File("new file name");
photo.renameTo(newFile);
于 2013-10-04T18:56:21.353 回答