2

I'm getting a audio file which I'm saving in a uri (android.net.Uri)

I need to display it's size in a TextView. I tried like this:

This is where I get the file from users library:

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

        Intent selectIntent = new Intent(Intent.ACTION_GET_CONTENT);
        selectIntent.setType("audio/*");
        startActivityForResult(selectIntent, AUDIO_REQUEST_CODE);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AUDIO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if ((data != null) && (data.getData() != null)) {
                audio = data.getData();
            }
        }
    }

Then I pass it to the next Activity like this:

Intent debugIntent = new Intent(this, Debug.class);
        Bundle bundle = new Bundle();
        bundle.putString("audio", audio.toString());
        debugIntent.putExtras(bundle);
        startActivity(debugIntent);

And use it in the debug activity like this:

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

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    audio = Uri.parse((String) bundle.get("audio"));

    File file = new File(audio.getPath());
    long size = file.length();
    if (file.exists()) {
        Toast.makeText(this, "exists", Toast.LENGTH_SHORT).show();
    }
    filesize = (TextView) findViewById(R.id.file_size);
    filesize.setText("file size: ".concat(String.valueOf(size)));

}

file.length() returns 0.

How can I fix that?

Daniele
  • 4,163
  • 7
  • 44
  • 95
  • 2
    file.length() will return 0L if file doesn't exists ( https://developer.android.com/reference/java/io/File.html#length() ). Make sure your file exists, you can use file.exists() method. – 0x3d Aug 09 '17 at 11:48
  • how do you initialize audio? – bendaf Aug 09 '17 at 11:48
  • @daniftodi you are right. The file seams not to exist. Any clue why this could happen? – Daniele Aug 09 '17 at 13:02
  • @pskink how exactly should I write it? `There's no ContentResolver.query` what's whit the # – Daniele Aug 09 '17 at 13:52
  • @Daniele who creates file ( extras name: audio ) that is sent between activities ? I think that previous activity didn't created "audio" file. Check if before opening current activity, "audio" file is created. – 0x3d Aug 09 '17 at 14:02
  • @daniftodi I get it from the users library in the main activity the pass it to this one usinbg Intent and Bundle – Daniele Aug 09 '17 at 14:04
  • @daniftodi Please have a look at the updated answer with all code – Daniele Aug 09 '17 at 14:09
  • I didn't understand how to call it. Can you explain yourself better – Daniele Aug 09 '17 at 15:15
  • @Daniele could you output in console value of audio.toString() and bundle.get("audio") and paste it here ? – 0x3d Aug 10 '17 at 06:45
  • I finally fixed this. thanks everyone for the help – Daniele Aug 10 '17 at 06:50

1 Answers1

3

In the end I fixed it like this:

private String getRealSizeFromUri(Context context, Uri uri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Audio.Media.SIZE };
            cursor = context.getContentResolver().query(uri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

I guess that's what pskink in the comments was saying but didn't bother to explain...

Anyway this is a viable solution which hopefully will be useful for other users too

Daniele
  • 4,163
  • 7
  • 44
  • 95