Back to project page FileDiv.
The source code is released under:
MIT License
If you think the Android project FileDiv listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.katsuaki.oshio.filediv; //from w w w. j a va2 s.c om import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity implements Runnable{ private static final boolean DEBUG = false; private static final String TAG = "FileDiv"; private Intent mIntent; private TextView mFileNameText; private EditText mSizeEditText; private Button mDivideButton; private Runnable mRunnable; private ProgressDialog mDialog; private String mFileName; private String mFilePath; private int mFileSize; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIntent = getIntent(); mRunnable = this; mFileName = mIntent.getStringExtra(Intent.EXTRA_SUBJECT); Uri uri = mIntent.getParcelableExtra(Intent.EXTRA_STREAM); mFilePath = uri.getPath(); // Display target file name. mFileNameText = (TextView)findViewById(R.id.textview_filename); mFileSize = get_file_size(mFilePath); mFileNameText.setText(mFilePath + " : " + get_size_str(mFileSize)); // Divide file size. mSizeEditText = (EditText)findViewById(R.id.edittext_mb); mSizeEditText.setText("3"); // Dialog mDialog = new ProgressDialog(this); mDialog.setTitle("Divide"); mDialog.setMessage("wait..."); mDialog.setIndeterminate(false); mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); mDivideButton = (Button)findViewById(R.id.button_divide); mDivideButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mDialog.show(); Thread thread = new Thread(mRunnable); thread.start(); } }); } @Override public void run() { divide(); mDialog.dismiss(); // Close Activity. finish(); } private void divide() { if (DEBUG) Log.i(TAG, "divide"); final int DIVIDE_SIZE = Integer.parseInt(mSizeEditText.getText().toString()) * 1024 * 1024; final int DIVIDE_NUM = (mFileSize + DIVIDE_SIZE - 1) / DIVIDE_SIZE; final int last = mFileSize % DIVIDE_SIZE; if (DEBUG) Log.i(TAG, "div size " + DIVIDE_SIZE + " num " + DIVIDE_NUM + " last " + last); FileInputStream fis = null; try { fis = new FileInputStream(mFilePath); BufferedInputStream in = new BufferedInputStream(fis); byte buf[] = new byte[DIVIDE_SIZE]; for (int i = 0; i < DIVIDE_NUM; i++) { int read_size = DIVIDE_SIZE; if (i == DIVIDE_NUM - 1) { read_size = last; } // Read int l = in.read(buf, 0, read_size); if (DEBUG) Log.i(TAG, "read " + l + " bytes"); // Write 1 file. FileOutputStream fos = new FileOutputStream(mFilePath + ".frac" + i); BufferedOutputStream out = new BufferedOutputStream(fos); out.write(buf, 0, l); if (DEBUG) Log.i(TAG, "write " + l + " bytes"); out.close(); fos.close(); } in.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } private int get_file_size(String file_name) { int size = 0; FileInputStream fis = null; try { fis = new FileInputStream(file_name); size = fis.available(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return size; } private String get_size_str(int size) { String size_str; if (size > 1024 * 1024) { size_str = Float.toString(size / 1024f / 1024f) + "MB"; size_str += " (" + Integer.toString(size) + "B)"; } else if (size > 1024) { size_str = Float.toString(size / 1024f) + "KB"; size_str += " (" + Integer.toString(size) + "B)"; } else { size_str = Integer.toString(size) + "B"; } return size_str; } }