Android 如何在viewpager中旋转图像并在现有路径上保存位图?

Android 如何在viewpager中旋转图像并在现有路径上保存位图?,android,bitmap,android-viewpager,image-rotation,Android,Bitmap,Android Viewpager,Image Rotation,我正在做一个类似画廊的应用程序 在这里,我使用view pager来显示存储在内存中的图像,我有五个底部导航菜单 现在我要做的是在“旋转”菜单上旋转图像,单击并将旋转后的位图保存到现有路径 我找了很多,但运气不好 在菜单上单击: new AsyncTask<Void,Void,Void>(){ @Override protected Void doInBackground(Void... params) { try { Bit

我正在做一个类似画廊的应用程序

在这里,我使用view pager来显示存储在内存中的图像,我有五个底部导航菜单

现在我要做的是在“旋转”菜单上旋转图像,单击并将旋转后的位图保存到现有路径

我找了很多,但运气不好

在菜单上单击:

new AsyncTask<Void,Void,Void>(){

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Bitmap myBitmap = BitmapFactory.decodeFile(Constant.image_paths.get(viewPager.getCurrentItem()));
            Bitmap newBit = rotate(myBitmap,45);
            saveToInternalStorage(newBit,Constant.image_paths.get(viewPager.getCurrentItem()));
        } catch (Exception e){
            Log.d("error","error in rotate");
        }
        return null;
   }

   @Override
   protected void onPostExecute(Void aVoid) {
       super.onPostExecute(aVoid);
       myViewPagerAdapter.notifyDataSetChanged();
   }

}.execute();




public static Bitmap rotate(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(),source.getHeight(), matrix, false);
}
private String saveToInternalStorage(Bitmap bitmapImage,String path) throws IOException {
    ContextWrapper cw = new ContextWrapper(getActivity());
    // path to /data/data/yourapp/app_data/imageDir
 //   File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath = new File(path);

    if(mypath.exists()){
        mypath.delete();
    }

    if(!mypath.exists()){
        mypath.createNewFile();
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return mypath.getAbsolutePath();
}    
newasynctask(){
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
位图myBitmap=BitmapFactory.decodeFile(Constant.image_path.get(viewPager.getCurrentItem());
位图newBit=旋转(myBitmap,45);
saveToInternalStorage(newBit、Constant.image_path.get(viewPager.getCurrentItem());
}捕获(例外e){
Log.d(“错误”、“旋转错误”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
myViewPagerAdapter.notifyDataSetChanged();
}
}.execute();
公共静态位图旋转(位图源、浮动角度){
矩阵=新矩阵();
矩阵。旋转后(角度);
返回Bitmap.createBitmap(source,0,0,source.getWidth(),source.getHeight(),matrix,false);
}
私有字符串存储到内部存储(位图位图位图图像,字符串路径)引发IOException{
ContextWrapper cw=新的ContextWrapper(getActivity());
///data/data/yourapp/app_data/imageDir的路径
//File directory=cw.getDir(“imageDir”,Context.MODE\u PRIVATE);
//创建imageDir
文件mypath=新文件(路径);
if(mypath.exists()){
mypath.delete();
}
如果(!mypath.exists()){
mypath.createNewFile();
}
FileOutputStream=null;
试一试{
fos=新文件输出流(mypath);
//使用位图对象上的压缩方法将图像写入输出流
bitmapImage.compress(Bitmap.CompressFormat.PNG,100,fos);
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
fos.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回mypath.getAbsolutePath();
}    

仅凭此代码无法实现此目标,要实现此目标,您需要遵循以下步骤:

  • 将图像转换为从内部存储器访问的位图
  • 将该位图传递给上面发布的“rotate()”方法
  • Rotate方法将返回一个新位图,通过“FileOutputStream”将该位图保存在您的存储器中,保存路径是您第一次获取图像的路径
  • 如果您不知道如何保存图像,请查看以下链接:


    仍然无法工作我已编辑了我的问题请看一看,是显示错误还是在某个时候导致应用程序崩溃?请在这里发布日志Cat的结果。这样我就知道问题出在哪里了。