Android Open Source - ChitChat File Helper






From Project

Back to project page ChitChat.

License

The source code is released under:

MIT License

If you think the Android project ChitChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.shivamb7.chitchat.workers;
//from  w  ww  . ja v a 2 s . c om
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import com.shivamb7.chitchat.R;

public class FileHelper {
  
  public static final String TAG = FileHelper.class.getSimpleName();
  
  public static final int SHORT_SIDE_TARGET = 1280;
  
  public static byte[] getByteArrayFromFile(Context context, Uri uri) {
    byte[] fileBytes = null;
        InputStream inStream = null;
        ByteArrayOutputStream outStream = null;
        
        if (uri.getScheme().equals("content")) {
          try {
            inStream = context.getContentResolver().openInputStream(uri);
            outStream = new ByteArrayOutputStream();
            
            byte[] bytesFromFile = new byte[1024*1024]; // buffer size (1 MB)
            int bytesRead = inStream.read(bytesFromFile);
            while (bytesRead != -1) {
              outStream.write(bytesFromFile, 0, bytesRead);
              bytesRead = inStream.read(bytesFromFile);
            }
            
            fileBytes = outStream.toByteArray();
          }
          catch (IOException e) {
            Log.e(TAG, e.getMessage());
          }
          finally {
            try {
              inStream.close();
              outStream.close();
            }
            catch (IOException e) { /*( Intentionally blank */ }
          }
        }
        else {
          try {
            File file = new File(uri.getPath());
            FileInputStream fileInput = new FileInputStream(file);
            fileBytes = IOUtils.toByteArray(fileInput);
          }
          catch (IOException e) {
            Log.e(TAG, e.getMessage());
          }
         }
        
        return fileBytes;
  }
  
  public static byte[] reduceImageForUpload(byte[] imageData) {
    Bitmap bitmap = ImageResizer.resizeImageMaintainAspectRatio(imageData, SHORT_SIDE_TARGET);
    
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outputStream);
    byte[] reducedData = outputStream.toByteArray();
    try {
      outputStream.close();
    }
    catch (IOException e) {
      // Intentionally blank
    }
    
    return reducedData;
  }

  public static String getFileName(Context context, Uri uri, String fileType) {
    String fileName = "uploaded_file.";
    
    if (fileType.equals(Constants.TYPE_PICTURE)) {
      fileName += "png";
    }
    else {
      // For video, we want to get the actual file extension
      if (uri.getScheme().equals("content")) {
        // do it using the mime type
        String mimeType = context.getContentResolver().getType(uri);
        int slashIndex = mimeType.indexOf("/");
        String fileExtension = mimeType.substring(slashIndex + 1);
        fileName += fileExtension;
      }
      else {
        fileName = uri.getLastPathSegment();
      }
    }
    
    return fileName;
  }
}




Java Source Code List

com.pkmmte.circularimageview.CircularImageView.java
com.shivamb7.chitchat.AddFriendsActivity.java
com.shivamb7.chitchat.ChatsActivity.java
com.shivamb7.chitchat.ComposeTextActivity.java
com.shivamb7.chitchat.ImageMessageActivity.java
com.shivamb7.chitchat.MainActivity.java
com.shivamb7.chitchat.ProfileActivity.java
com.shivamb7.chitchat.RecipientsActivity.java
com.shivamb7.chitchat.RegisterActivity2.java
com.shivamb7.chitchat.RegisterActivity.java
com.shivamb7.chitchat.adapters.ChatsAdapter.java
com.shivamb7.chitchat.adapters.FriendGridAdapter.java
com.shivamb7.chitchat.adapters.LevelAdapter.java
com.shivamb7.chitchat.fragments.ChatsFragment.java
com.shivamb7.chitchat.fragments.ContactsFragment.java
com.shivamb7.chitchat.fragments.ProfileFragment.java
com.shivamb7.chitchat.workers.ChitChatApplication.java
com.shivamb7.chitchat.workers.CircularImageView.java
com.shivamb7.chitchat.workers.Constants.java
com.shivamb7.chitchat.workers.Fab.java
com.shivamb7.chitchat.workers.FileHelper.java
com.shivamb7.chitchat.workers.ImageResizer.java
com.shivamb7.chitchat.workers.Level.java
com.shivamb7.chitchat.workers.RippleView.java
com.shivamb7.chitchat.workers.TypefaceSpan.java