Android应用程序在尝试将音频上传到Parse.com时崩溃
嗨,这是我在这里的第一篇文章&我迫切需要帮助解决这个问题。每当我只上传图像时,它工作正常,但一旦我尝试上传音频文件,我的应用程序就会崩溃,logcat显示没有错误或任何东西。谁能帮帮我吗。(我知道每个文件的文件大小限制为10 MB,并且我已经确保我的音频文件比这个小。)提前谢谢Android应用程序在尝试将音频上传到Parse.com时崩溃,android,file,audio,file-upload,parse-platform,Android,File,Audio,File Upload,Parse Platform,嗨,这是我在这里的第一篇文章&我迫切需要帮助解决这个问题。每当我只上传图像时,它工作正常,但一旦我尝试上传音频文件,我的应用程序就会崩溃,logcat显示没有错误或任何东西。谁能帮帮我吗。(我知道每个文件的文件大小限制为10 MB,并且我已经确保我的音频文件比这个小。)提前谢谢 private static final int SELECT_AUDIO = 1; private Uri audioUri; String selectedPath = ""; File audio; private
private static final int SELECT_AUDIO = 1;
private Uri audioUri;
String selectedPath = "";
File audio;
private static final int PICK_FROM_FILE = 2;
Button btn_choose_image;
Bitmap bitmap;
private Uri imageCaptureUri;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_podcast);
imageView = (ImageView) findViewById(R.id.pART);
btn_choose_image = (Button) findViewById(R.id.GALLERY);
btn_choose_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
});
TextView textView = (TextView) findViewById(R.id.pARTIST);
textView.setText(MainActivity.NAME);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
bitmap = null;
String path = "";
if (requestCode == PICK_FROM_FILE) {
imageCaptureUri = data.getData();
try {
bitmap = getBitmapFromUri(imageCaptureUri);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
else if (requestCode == SELECT_AUDIO) {
try {
audioUri = data.getData();
audio = new File(audioUri.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return bitmap;
}
//TODO: Upload audio files
public void uploadAudio(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, SELECT_AUDIO);
}
public void uploadAll(View view) {
//Toast.makeText(UploadPodcast.this, "Uploaded", Toast.LENGTH_LONG).show();
EditText editText = (EditText) findViewById(R.id.pName);
ParseObject podcast = new ParseObject("Podcast");
uploadImageToParse(bitmap, podcast, "albumArt");
finish();
}
private ParseObject uploadAudioToParse(File audioFile, ParseObject po, String columnName){
if(audioFile != null){
Log.d("EB", "audioFile is not NULL: " + audioFile.toString());
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(audioFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int read;
byte[] buff = new byte[1024];
try {
while ((read = in.read(buff)) > 0)
{
out.write(buff, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
byte[] audioBytes = out.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile(audioFile.getName() , audioBytes);
po.put(columnName, file);
// Upload the file into Parse Cloud
file.saveInBackground();
po.saveInBackground();
}
return po;
}
private void uploadImageToParse(Bitmap bitmap, ParseObject po, String columnName) {
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile("podcastArt.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a column named "ImageFile" and insert the image
po.put("albumArt", file);
// Create the class and the columns
po.saveInBackground();
// Show a simple toast message
Toast.makeText(UploadPodcast.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
我甚至没有调用uploadAudioToParse()函数,但如果我选择了一个音频文件,当我调用uploadAll()时,应用程序会崩溃。如果我只选择一个图像而不是音频文件,应用程序就可以正常工作。
(另外,我有不同的按钮来选择图像和音频文件&我还想将它们都上传到我的解析数据库中的同一个元组。)
编辑:这是日志 11-30 01:54:21.586 16550-16550/sas.带系统的拔下插头。错误: java.io.FileNotFoundException:/external/audio/media/124081:打开 失败:enoint(没有这样的文件或目录)11-30 01:54:21.593 16550-16550/sas.W/System.err:处于断开状态 libcore.io.IoBridge.open(IoBridge.java:452)11-30 01:54:21.593 16550-16550/sas.W/System.err:处于断开状态 FileInputStream.(FileInputStream.java:76)11-30 01:54:21.593 16550-16550/sas。拔下带系统的插头。错误:at uploadAudioToParse(UploadPodcast.java:138) 11-30 01:54:21.593 16550-16550/sas。拔下系统插头。错误:在 sas.plug.UploadPodcast.uploadAll(UploadPodcast.java:128)11-30 01:54:21.593 16550-16550/sas。拔下带系统的插头。错误:at java.lang.reflect.Method.invoke(本机方法)11-30 01:54:21.593 16550-16550/sas.W/System.err:处于断开状态 view.view$DeclaredOnClickListener.onClick(view.java:4447) 11-30 01:54:21.594 16550-16550/sas。拔下系统插头。错误:在 android.view.view.performClick(view.java:5198)11-30 01:54:21.594 16550-16550/sas.W/System.err:处于断开状态 android.view.view$PerformClick.run(view.java:21147)11-30 01:54:21.594 16550-16550/sas.W/System.err:处于断开状态 android.os.Handler.handleCallback(Handler.java:739)11-30 01:54:21.594 16550-16550/sas.W/System.err:处于断开状态 android.os.Handler.dispatchMessage(Handler.java:95)11-30 01:54:21.594 16550-16550/sas.W/System.err:处于断开状态 android.os.Looper.loop(Looper.java:148)11-30 01:54:21.594 16550-16550/sas.W/System.err:处于断开状态 android.app.ActivityThread.main(ActivityThread.java:5417)11-30 01:54:21.594 16550-16550/sas。拔下系统插头。错误:at java.lang.reflect.Method.invoke(本机方法)11-30 01:54:21.594 16550-16550/sas.W/System.err:处于断开状态 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 11-30 01:54:21.594 16550-16550/sas。拔下系统插头。错误:在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)11-30 01:54:21.594 16550-16550/sas。拔下系统插头。错误:由以下原因引起: android.system.ErrnoException:open失败:enoint(没有这样的文件或 目录)11-30 01:54:21.596 16550-16550/sas.plug W/System.err:
在libcore.io.Posix.open(本机方法)11-30 01:54:21.596 16550-16550/sas.W/System.err:处于断开状态 libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)11-30 01:54:21.596 16550-16550/sas.W/System.err:处于断开状态 libcore.io.IoBridge.open(IoBridge.java:438)11-30 01:54:21.596 16550-16550/sas.plug W/System.err:。。。14更多11-30 01:54:21.597 16550-16550/sas.D拔出/AndroidRuntime:关闭VM 11-30 01:54:21.598 16550-16550/sas.拔下E/AndroidRuntime:致命异常: 主要的 进程:sas.plug,PID:16550 java.lang.IllegalStateException:无法执行的方法 安卓:onClick 在android.view.view$DeclaredOnClickListener.onClick(view.java:4452) 在android.view.view.performClick上(view.java:5198) 在android.view.view$PerformClick.run(view.java:21147) 位于android.os.Handler.handleCallback(Handler.java:739) 位于android.os.Handler.dispatchMessage(Handler.java:95) 位于android.os.Looper.loop(Looper.java:148) 位于android.app.ActivityThread.main(ActivityThread.java:5417) 位于java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:726) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 原因:java.lang.reflect.InvocationTargetException 位于java.lang.reflect.Method.invoke(本机方法) 在android.view.view$DeclaredOnClickListener.onClick(view.java:4447) 在android.view.view.performClick上(view.java:5198) 在android.view.view$PerformClick.run(view.java:21147) 位于android.os.Handler.handleCallback(Handler.java:739) 位于android.os.Handler.dispatchMessage(Handler.java:95) 在android.os上
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_floating_material_dark"
android:fitsSystemWindows="true"
tools:context="sas.unplug.UploadPodcast">
<EditText
android:id="@+id/pName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:hint="Podcast Name"
android:textColor="#64ffffff"
android:textColorHint="#64ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pName"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="by"
android:textColor="#ff5252"
android:textSize="14sp" />
<TextView
android:id="@+id/pARTIST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/by"
android:layout_toRightOf="@+id/by"
android:paddingLeft="16dp"
android:text="Artist"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#64ffffff" />
<ImageView
android:id="@+id/pART"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/GALLERY"
android:scaleType="centerCrop"
android:src="@drawable/zghost" />
<Button
android:id="@+id/GALLERY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@+id/pARTIST"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="?android:attr/selectableItemBackground"
android:text="Choose image from gallery"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pART"
android:layout_marginTop="25dp"
android:gravity="center">
<Button
android:id="@+id/pSELECT"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="?android:attr/selectableItemBackground"
android:onClick="uploadAudio"
android:text="Select podcast"
android:textColor="#ffffff" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="uploadAll"
android:text="Upload podcast" />
</LinearLayout>
</RelativeLayout>
// Upload the file into Parse Cloud
file.saveInBackground();
po.saveInBackground();
file.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e != null) {
Log.e(TAG, "file upload failed", e);
} else {
// now parse knows that file exists and is able to
// to make the association with your po object
po.saveInBackground();
}
}
});
final ParseObject parseObject = new ParseObject("CLASS_NAME");
File audioFile = new File(audioUri.getPath());
final ParseFile parseFile = new ParseFile(audioFile);
parseObject.add("FILE_COLUMN", parseFile);
parseFile.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e == null){
parseObject.saveInBackground();
}
}
});