Monday, March 13, 2017

Android - Read Files, Apps, photos & media from Android phone

In this blog, i'm going to write a small code snippet that fetches some useful content from Android Phone.

Below is the snippet


1. Set up Application permissions.


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


2. Get install Application info (APK).      


 In your Activity/Fragment call following method that returns a list of install Application.

      public List<AppInfo> getInstalledApps(Context mContext) {

            List<PackageInfo> apps = mContext.getPackageManager().getInstalledPackages(0);
            ArrayList<AppInfo> mInstalledApps = new ArrayList();

            for (PackageInfo p : apps) {
                if (!isSystemPackage(p)) {
                    AppInfo newInfo = new AppInfo();
                    newInfo.setAppName(p.applicationInfo.loadLabel(mContext.getPackageManager()).toString());
                    newInfo.setPackageName(p.packageName);
                    newInfo.setAapVersion(p.versionName);
                    newInfo.setAppVersionCode(p.versionCode);
                    newInfo.setAppAPKUrl(p.applicationInfo.publicSourceDir);
                    newInfo.setAppIcon(p.applicationInfo.loadIcon(mContext.getPackageManager()));

                    mInstalledApps.add(newInfo);
                }
            }
        }
        return mInstalledApps;
    }

private boolean isSystemPackage(PackageInfo packageInfo) {
        return (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    }

2. Get Video.


In this snippet, We fetch all video in Android Phone.

 public List<MediaModel> getVideos(Context mContext){
   
            ArrayList<MediaModel> mVideos = new ArrayList<>();
            String[] videoProjection = {MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DATA,
                    MediaStore.Video.Media.DISPLAY_NAME,
                    MediaStore.Video.Media.SIZE,
                    MediaStore.Video.Media.SIZE};

            makeList(mContext.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoProjection, null, null, null), mVideos);
        return mVideos;
    }

3. Get Audio.


In this snippet, We fetch mp3 and wav Audio files from Android Phone.

public List<MediaModel> getAudios(Context mContext) {

            ArrayList<MediaModel> mAudios = new ArrayList<>();

            String[] videoProjection = {MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.ALBUM_ID};

            makeList(mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, videoProjection,
MediaStore.Files.FileColumns.MIME_TYPE + "=?",
                    new String[]{MimeTypeMap.getSingleton().getMimeTypeFromExtension(mContext.getResources().getString(R.string.wav))}, null), mAudios);

            makeList(mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, videoProjection, MediaStore.Files.FileColumns.MIME_TYPE + "=?",
                    new String[]{MimeTypeMap.getSingleton().getMimeTypeFromExtension(mContext.getResources().getString(R.string.mp3))}, null), mAudios);
   

        return mAudios;
    }

4. Get Image.


In this snippet, We fetch all images from Android Phone.

public List<MediaModel> getImages() {
        if (mImages == null || mImages.isEmpty()) {
            mImages = new ArrayList<>();
            String[] videoProjection = {MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.SIZE};

            makeList(mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, videoProjection, null, null, null), mImages);
        }
        return mImages;
    }


5. Text Files.


In this snippet, We fetch only PDF files from Android Phone. You can get all files by changes just extension in below snippet.

public List<MediaModel> getFiles() {

        if (mFiles == null || mFiles.isEmpty()) {
            mFiles = new ArrayList();

            String[] videoProjection = {MediaStore.Files.FileColumns._ID,
                    MediaStore.Files.FileColumns.DATA,
                    MediaStore.Files.FileColumns.DISPLAY_NAME,
                    MediaStore.Files.FileColumns.SIZE};

            makeList(mContext.getContentResolver().query(MediaStore.Files.getContentUri(mContext.getResources().getString(R.string.external)), videoProjection, MediaStore.Files.FileColumns.MIME_TYPE + "=?",
                    new String[]{MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf")}, null), mFiles);

        return mFiles;
    }


Method and Model classes that are used in the above snippets.


A). makeList(@NonNull Cursor audioFilesCursor, ArrayList<MediaModel> arrayList) 

private void makeList(@NonNull Cursor audioFilesCursor, ArrayList<MediaModel> arrayList) {
        audioFilesCursor.moveToFirst();
        while (!audioFilesCursor.isAfterLast()) {
            MediaModel mediaModel = new MediaModel(audioFilesCursor.getString(1), new File(audioFilesCursor.getString(1)).getName());
            if (mHomeContract.isSelected(audioFilesCursor.getString(1))) {
                mediaModel.setIsSelected(true);
            }
            arrayList.add(mediaModel);
            audioFilesCursor.moveToNext();
        }
        audioFilesCursor.close();
    }

B). MediaModel

public class MediaModel {
    private String url;
    private String name;
    private boolean isSelected;

    /**
     * Instantiates a new Audio model.
     *
     * @param fileUrl  the audio url
     * @param fileName the audio name
     */
    public MediaModel(String fileUrl, String fileName) {
        url = fileUrl;
        name = fileName;
    }

    /**
     * Gets url.
     *
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * Gets name.
     *
     * @return the name
     */
    public String getName() {

        return name;
    }

    /**
     * Is selected boolean.
     *
     * @return the boolean
     */
    public boolean isSelected() {
        return isSelected;
    }

    /**
     * Sets is selected.
     *
     * @param isSelected the is selected
     */
    public void setIsSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }
}

C). Create a model that keep information of install Application in your device. 

 public class AppInfo {
              private String appAPKUrl;
              private String appName;
              private String appPackage;
              private String aapVersion;
              private int appVersionCode;
              private Drawable appIcon;
              private boolean isSelected;

              public String getAppAPKUrl() {
                 return appAPKUrl;
              }

             public String getAppName() {
                 return appName;
             }

             public String getPackageName() {
                 return appPackage;
             }

             public String getAapVersion() {
                return aapVersion;
             }

             public int getAppVersionCode() {
                return appVersionCode;
             }

             public Drawable getAppIcon() {
                 return appIcon;
             }


            public boolean isSelected() {
                 return isSelected;
            }

           public void setIsSelected(boolean isSelected) {
               this.isSelected = isSelected;
           }

          public void setAppAPKUrl(String appAPKUrl) {
              this.appAPKUrl = appAPKUrl;
          }

          public void setAppName(String appName) {
               this.appName = appName;
          }

         public void setPackageName(String packageName) {
                this.appPackage = packageName;
         }

        public void setAapVersion(String aapVersion) {
             this.aapVersion = aapVersion;
        }

       public void setAppVersionCode(int appVersionCode) {
         this.appVersionCode = appVersionCode;
       }

       public void setAppIcon(Drawable appIcon) {
         this.appIcon = appIcon;
      }
  }
Share:

0 comments:

Post a Comment

We're Social

Categories

Ad here

  • Cover art Blend

    Blend app is very advanced app there are lots of features like shayari, status, chat, feed....