相关疑难解决方法(0)

android - 将图像保存到图库中

我有一个带有图片库的应用程序,我希望用户可以将其保存到自己的图库中.我已经创建了一个带有单个声音"保存"的选项菜单,但问题是......我怎样才能将图像保存到图库中?

这是我的代码:

@Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle item selection
            switch (item.getItemId()) {
            case R.id.menuFinale:

                imgView.setDrawingCacheEnabled(true);
                Bitmap bitmap = imgView.getDrawingCache();
                File root = Environment.getExternalStorageDirectory();
                File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
                try 
                {
                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);
                    bitmap.compress(CompressFormat.JPEG, 100, ostream);
                    ostream.close();
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }



                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }
Run Code Online (Sandbox Code Playgroud)

我不确定这部分代码:

File root = Environment.getExternalStorageDirectory();
                File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
Run Code Online (Sandbox Code Playgroud)

保存到画廊是否正确?不幸的是代码不起作用:(

android gallery save-image imageview

83
推荐指数
6
解决办法
16万
查看次数

Android ACTION_MEDIA_SCANNER_SCAN_FILE直到重启才显示图像?

我目前正在开发一个Android应用程序,其中用户拍摄存储在设备上的外部存储器中的照片,然后我也试图让图库扫描文件以将新媒体添加到图库但是这似乎不会更新直到设备重启.

我使用的代码如下:

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Log.v("INFO", mCurrentPhotoPath.toString());
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
Run Code Online (Sandbox Code Playgroud)

使用我的应用程序拍照后,我可以使用设备上的文件管理器应用程序验证它是否存在,并且在将文件传递给Media Scanner之前记录它看起来是正确的文件路径

file:/storage/emulated/0/Pictures/JPEG_20140712_163043_1418054327.jpg
Run Code Online (Sandbox Code Playgroud)

谢谢亚伦

android android-intent android-mediascanner

7
推荐指数
1
解决办法
1万
查看次数

文件未添加到图库android

我正在制作的这个应用程序正在拍照并将其保存到SD卡但图片未显示在库中......我的代码是否有问题

public void takepicture(View view){
     try{
           String state = Environment.getExternalStorageState();
             if (Environment.MEDIA_MOUNTED.equals(state)) {
                    //To store public files
                File directory=new File(Environment.getExternalStorageDirectory()
                        , "Myapp Pictures");                        
                    if(!directory.exists())
                        directory.mkdir();
                // Create an image file name
                    String timeStamp = 
                        new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    String imageFileName = "Img" + timeStamp + ".jpg";
                        file=new File(directory, imageFileName);
                   if(!file.exists())
                       file.createNewFile();                    
             }
    }catch(Exception e){
    e.getCause();   
    }
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    mImageUri=Uri.fromFile(file);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    startActivityForResult(takePictureIntent, actioncode);          
    }
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {                             
            if(requestCode == actioncode){                  
                    if((file.length())==0){
                        file.delete(); …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-gallery android-imageview android-mediascanner

2
推荐指数
1
解决办法
7775
查看次数