2017-07-17 16 views
-1

写真を撮って特定のフォルダに保存し、リストビューで取得しようとしています。私は特定のフォルダにそれを格納するためのショーである最初の部分を行ったが、私はそれを取得する方法を知らない。特定のフォルダから画像を取り込む方法は?

ここで撮影された画像は、写真の内側にあるhello cameraフォルダに保存されます。下のコードはビデオを録画することもできます。特定のフォルダからのみイメージを取得するコードを教えてください。私はこのコードが必要なのは、セキュリティで文書を保管するためです。保存画像

public class photo extends Activity { 


    // Activity request codes 
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; 
    private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200; 
    public static final int MEDIA_TYPE_IMAGE = 1; 
    public static final int MEDIA_TYPE_VIDEO = 2; 

    // directory name to store captured images and videos 
    private static final String IMAGE_DIRECTORY_NAME = "Hello Camera"; 



    Button submit; 
    Context context; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_photo); 

     context = this; 

     imgPreview = (ImageView) findViewById(R.id.imgPreview); 
     videoPreview = (VideoView) findViewById(R.id.videoPreview); 
     btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture); 
     btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo); 
     submit = (Button) findViewById(R.id.button2); 
     name = (EditText) findViewById(R.id.imagename); 
     //n1= (EditText) findViewById(R.id.imagename); 


//  final String Uname = name.getText().toString(); 

     /** 
     * Capture image button click event 
     */ 
     Calendar cal; 
     cal = Calendar.getInstance(); 

     String currDate = cal.get(Calendar.DATE) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.YEAR); 

     mydb = this.openOrCreateDatabase(DataProvider.DATABASE_NAME, Context.MODE_PRIVATE, null); 
     Cursor cs = mydb.rawQuery("select * from '" + DataProvider.TBL_PRE + "'", null); 




//  cs = getContentResolver().query(DataProvider.PRE_URI, null,DataProvider.DATE+"="+currDate, null, null); 


     btnCapturePicture.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // capture picture 
       captureImage(); 
      } 
     }); 


     submit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String err = val(); 
       if (err.length() <= 0) { 

        String uname = name.getText().toString(); 
        System.out.println("++uname--->" + uname); 
        ContentValues values = new ContentValues(); 
        values.put(DataProvider.NAME1, uname); 
        getContentResolver().insert(DataProvider.PRE_URI, values); 
        System.out.println("\n values" + values.toString()); 
        startActivity(new Intent(photo.this, preview.class)); 
        overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right); 




       } 
       else { 
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
        alertDialog.setTitle("Alert"); 
        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          dialog.cancel(); 
         } 
        }); 
        alertDialog.setMessage(err); 
        alertDialog.show(); 

       } 

      } 
     }); 

     /*submit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String err = val(); 
       if (err.length() <= 0) { 

        String uname = name.getText().toString(); 
        System.out.println("uname--->" + uname); 
        ContentValues values = new ContentValues(); 
        values.put(DataProvider.NAME1, uname); 
System.out.println("^^^"+uname); 

        //values.put(DataProvider.DATE, date.getText().toString()); 
        getContentResolver().insert(DataProvider.PRE_URI, values); 
        System.out.println("\n values" + values.toString()); 
        Intent i = new Intent(photo.this, preview.class); 
        startActivity(i);} 
      } 


     }); 

*/ 


     /** 
     * Record video button click event 
     */ 
     btnRecordVideo.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // record video 
       recordVideo(); 
      } 
     }); 

     // Checking camera availability 
     if (!isDeviceSupportCamera()) { 
      Toast.makeText(getApplicationContext(), 
        "Sorry! Your device doesn't support camera", 
        Toast.LENGTH_LONG).show(); 
      // will close the app if the device does't have camera 
      finish(); 
     } 


    } 

    /** 
       * Checking device has camera hardware or not 
       * */ 
    private boolean isDeviceSupportCamera() { 
     if (getApplicationContext().getPackageManager().hasSystemFeature(
       PackageManager.FEATURE_CAMERA)) { 
      // this device has a camera 
      return true; 
     } else { 
      // no camera on this device 
      return false; 
     } 
    } 

    /** 
    * Capturing Camera Image will lauch camera app requrest image capture 
    */ 
    private void captureImage() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

     // start the image capture Intent 
     startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
    } 

    /** 
    * Here we store the file url as it will be null after returning from camera 
    * app 
    */ 
    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 

     // save file url in bundle as it will be null on scren orientation 
     // changes 
     outState.putParcelable("file_uri", fileUri); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 

     // get the file url 
     fileUri = savedInstanceState.getParcelable("file_uri"); 
    } 

    /** 
    * Recording video 
    */ 
    private void recordVideo() { 
     Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); 

     fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 

     // set video quality 
     intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file 

     // name 

     // start the video capture Intent 
     startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE); 
    } 

    /** 
    * Receiving activity result method will be called after closing the camera 
    * */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // if the result is capturing Image 
     if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // successfully captured the image 
       // display it in image view 
       previewCapturedImage(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled Image capture 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to capture image 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // video successfully recorded 
       // preview the recorded video 
       previewVideo(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled recording 
       Toast.makeText(getApplicationContext(), 
         "User cancelled video recording", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to record video 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to record video", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
    } 

    /** 
    * Display image from a path to ImageView 
    */ 
    private void previewCapturedImage() { 
     try { 
      // hide video preview 
      videoPreview.setVisibility(View.GONE); 

      imgPreview.setVisibility(View.VISIBLE); 

      // bimatp factory 
      BitmapFactory.Options options = new BitmapFactory.Options(); 

      // downsizing image as it throws OutOfMemory Exception for larger 
      // images 
      options.inSampleSize = 8; 

      final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
        options); 

      imgPreview.setImageBitmap(bitmap); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Previewing recorded video 
    */ 
    private void previewVideo() { 
     try { 
      // hide image preview 
      imgPreview.setVisibility(View.GONE); 

      videoPreview.setVisibility(View.VISIBLE); 
      videoPreview.setVideoPath(fileUri.getPath()); 
      // start playing 
      videoPreview.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * ------------ Helper Methods ---------------------- 
    * */ 

    /** 
    * Creating file uri to store image/video 
    */ 
    public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /** 
    * returning image/video 
    */ 
    private static File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       IMAGE_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
         + IMAGE_DIRECTORY_NAME + " directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "IMG_" + timeStamp + ".jpg"); 
     } else if (type == MEDIA_TYPE_VIDEO) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "VID_" + timeStamp + ".mp4"); 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 

    public void onBackPressed() { 
     super.onBackPressed(); 

     startActivity(new Intent(photo.this,preview.class)); 
     overridePendingTransition(R.anim.pull_in_left,R.anim.push_out_right); 
     finish(); 
     System.exit(0); 
    } 


    public String val() { 
     String err = ""; 

     if (name.getText().toString().length() <= 0) { 
      err += "Enter the Name \n"; 
     } 




     return err; 
    } 

} 


     // name 

     // start the video capture Intent 
     startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE); 
    } 

    /** 
    * Receiving activity result method will be called after closing the camera 
    * */ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // if the result is capturing Image 
     if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // successfully captured the image 
       // display it in image view 
       previewCapturedImage(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled Image capture 
       Toast.makeText(getApplicationContext(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to capture image 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // video successfully recorded 
       // preview the recorded video 
       previewVideo(); 
      } else if (resultCode == RESULT_CANCELED) { 
       // user cancelled recording 
       Toast.makeText(getApplicationContext(), 
         "User cancelled video recording", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to record video 
       Toast.makeText(getApplicationContext(), 
         "Sorry! Failed to record video", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
    } 

    /** 
    * Display image from a path to ImageView 
    */ 
    private void previewCapturedImage() { 
     try { 
      // hide video preview 
      videoPreview.setVisibility(View.GONE); 

      imgPreview.setVisibility(View.VISIBLE); 

      // bimatp factory 
      BitmapFactory.Options options = new BitmapFactory.Options(); 

      // downsizing image as it throws OutOfMemory Exception for larger 
      // images 
      options.inSampleSize = 8; 

      final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
        options); 

      imgPreview.setImageBitmap(bitmap); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Previewing recorded video 
    */ 
    private void previewVideo() { 
     try { 
      // hide image preview 
      imgPreview.setVisibility(View.GONE); 

      videoPreview.setVisibility(View.VISIBLE); 
      videoPreview.setVideoPath(fileUri.getPath()); 
      // start playing 
      videoPreview.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * ------------ Helper Methods ---------------------- 
    * */ 

    /** 
    * Creating file uri to store image/video 
    */ 
    public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /** 
    * returning image/video 
    */ 
    private static File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       IMAGE_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
         + IMAGE_DIRECTORY_NAME + " directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.getDefault()).format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "IMG_" + timeStamp + ".jpg"); 
     } else if (type == MEDIA_TYPE_VIDEO) { 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator 
        + "VID_" + timeStamp + ".mp4"); 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 

    public void onBackPressed() { 
     super.onBackPressed(); 

     startActivity(new Intent(photo.this,preview.class)); 
     overridePendingTransition(R.anim.pull_in_left,R.anim.push_out_right); 
     finish(); 
     System.exit(0); 
    } 


    public String val() { 
     String err = ""; 

     if (name.getText().toString().length() <= 0) { 
      err += "Enter the Name \n"; 
     } 




     return err; 
    } 

} 
+0

可能な複製を許可を追加することを忘れないでくださいAndroid](https://stackoverflow.com/questions/14858694/retrieve-images-of-particular-folder-in-android) –

+0

ここで書式設定ツールを学んでください。段落テキストはコードブロックには入りませんが、コードブロックには_does_コードが入ります。 – halfer

+0

また、_Stack Overflow_は自由労働の清算機関ではないため、「コードを教えてください」という形式の質問はここでは話題にはなりません。 – halfer

答えて

0

ため

私のコードは、してみてください。この

private void getImages() { 
    String[] filenames = new String[0]; 
    File path = new File(Environment.getExternalStorageDirectory() + "/Favorite");// add here your fo;der name 
    if (path.exists()) { 
     filenames = path.list(); 
    } 
    for (int i = 0; i < filenames.length; i++) { 
     imagesPathArrayList.add(path.getPath() + "/" + filenames[i]); 
     Log.e("FAV_Images", imagesPathArrayList.get(i)); 


//Bitmap mBitmap = Bitmap.decodeFile(path.getPath()+"/"+ fileNames[i]); 
     ///Now set this bitmap on imageview 
    } 
} 

マニフェストファイル内の特定のフォルダの画像を取得[の

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

何がimageAdapterです..エラーを取得しています...私は完全なコードを教えてください – shwettha

+0

@shwettha申し訳ありません、その行を削除してください –

+0

imagesPathArrayList:エラーを解決できません。 ..pls助けて – shwettha

関連する問題